﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using DHQGHN.PM2.Models.Online;
using DHQGHN.PM2.Models;
using DHQGHN.PM2.ViewModels.Online;
using System.Runtime.Serialization.Json;
using BotDetect.Web.UI.Mvc;
using BotDetect.Web;
using System.Collections;

#region
namespace DHQGHN.PM2.Web.HtmlHelpers
{

    public static class HtmlHelperExtensions
    {

        public static MvcHtmlString BootstrapPager(System.Web.Mvc.HtmlHelper helper, int currentPageIndex, Func<int, string> action, int totalItems, int pageSize = 10, int numberOfLinks = 5)
        {

            if (totalItems <= 0)
            {

                return MvcHtmlString.Empty;

            }

            var totalPages = (int)Math.Ceiling(totalItems / (double)pageSize);

            var lastPageNumber = (int)Math.Ceiling((double)currentPageIndex / numberOfLinks) * numberOfLinks;
            var firstPageNumber = 0;
            if (numberOfLinks > 0)
            {
                firstPageNumber = lastPageNumber - (numberOfLinks - 1);
            }

            var hasPreviousPage = currentPageIndex > 1;

            var hasNextPage = currentPageIndex < totalPages;

            if (lastPageNumber > totalPages)
            {

                lastPageNumber = totalPages;

            }

            var ul = new TagBuilder("ul");

            ul.AddCssClass("pagination");

            ul.InnerHtml += AddLink(1, action, currentPageIndex == 1, "disabled", "<<", "First Page");

            ul.InnerHtml += AddLink(currentPageIndex - 1, action, !hasPreviousPage, "disabled", "<", "Previous Page");

            for (int i = firstPageNumber; i <= lastPageNumber; i++)
            {

                ul.InnerHtml += AddLink(i, action, i == currentPageIndex, "active", i.ToString(), i.ToString());

            }

            ul.InnerHtml += AddLink(currentPageIndex + 1, action, !hasNextPage, "disabled", ">", "Next Page");

            ul.InnerHtml += AddLink(totalPages, action, currentPageIndex == totalPages, "disabled", ">>", "Last Page");

            return MvcHtmlString.Create(ul.ToString());

        }



        private static TagBuilder AddLink(int index, Func<int, string> action, bool condition, string classToAdd, string linkText, string tooltip)
        {
            var li = new TagBuilder("li");

            li.MergeAttribute("title", tooltip);

            if (condition)
            {

                li.AddCssClass(classToAdd);

            }

            var a = new TagBuilder("a");

            a.MergeAttribute("href", !condition ? action(index) : "javascript:");

            a.SetInnerText(linkText);

            li.InnerHtml = a.ToString();

            return li;

        }

    }

}

#endregion

namespace DHQGHN.PM2.Controllers
{
    public class SessionUsers
    {
        public string HouseholdId { get; set; }
        public int LoginCount { get; set; }
        public string LoginPin { get; set; }
        public string LoginCode { get; set; }
    }
    //[SessionExpireFilterAttribute]
    public class OnlineController : BaseController
    {
        private const int NUMBER_OF_HOUSEHOLD_DEFAULT = 3;
        log4net.ILog logger = log4net.LogManager.GetLogger(typeof(OnlineController));
        //const int TIME_OUT = 1000000000;
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //base.OnActionExecuting(filterContext);

            List<string> lstAdmin = new List<string>();
            lstAdmin.Add("GetAdministrativeUnit");
            lstAdmin.Add("GetAdministrativeUnit1");
            lstAdmin.Add("GetAdministrativeUnit3");
            lstAdmin.Add("LoginWithCaptcha");
            lstAdmin.Add("LoginKhaiOnline");
            lstAdmin.Add("Login");
            List<string> lstActionNamNotCheck = new List<string>();
            //lstActionNamNotCheck.Add("LoginWithCaptcha");
            //lstActionNamNotCheck.Add("LoginKhaiOnline");
            lstActionNamNotCheck.Add("GetAdministrativeUnit");
            lstActionNamNotCheck.Add("GetAdministrativeUnit1");
            lstActionNamNotCheck.Add("GetAdministrativeUnit3");
            lstActionNamNotCheck.Add("GenerateCode");
            lstActionNamNotCheck.Add("ResetCodeLogin");
            lstActionNamNotCheck.Add("ResetCodeLoginHouseHold");
            lstActionNamNotCheck.Add("ResetCodeLoginHouseHold2");
            if (filterContext != null)
            {
                if ("Login".Equals(filterContext.ActionDescriptor.ActionName) || "LoginPost".Equals(filterContext.ActionDescriptor.ActionName))
                {

                    CheckTimeOut();
                }
                else
                {
                    if (Session["AuthToken"] != null && Request.Cookies["AuthToken"] != null && !lstActionNamNotCheck.Contains(filterContext.ActionDescriptor.ActionName))
                    {
                        if (!Session["AuthToken"].ToString().Equals(Request.Cookies["AuthToken"].Value))
                        {
                            filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
                            {
                                controller = "Online",
                                action = "Login"
                            }));
                        }
                        else
                        {
                            if (!"PinPost".Equals(filterContext.ActionDescriptor.ActionName) & !lstActionNamNotCheck.Contains(filterContext.ActionDescriptor.ActionName) & Session["UserInfoOnline"] == null)
                            {

                                Session.Clear();
                                Session.RemoveAll();
                                Session.Abandon();
                                if (Request.Cookies["ASP.NET_SessionId"] != null)
                                {
                                    Response.Cookies["ASP.NET_SessionId"].Value = string.Empty;
                                    Response.Cookies["ASP.NET_SessionId"].Expires = DateTime.Now.AddMonths(-20);
                                }

                                if (Request.Cookies["AuthToken"] != null)
                                {
                                    Response.Cookies["AuthToken"].Value = string.Empty;
                                    Response.Cookies["AuthToken"].Expires = DateTime.Now.AddMonths(-20);
                                }
                                filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
                                {
                                    controller = "Online",
                                    action = "Login"
                                }));

                            }
                        }
                    }
                    else
                    {
                        if (!lstActionNamNotCheck.Contains(filterContext.ActionDescriptor.ActionName))
                        {
                            filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
                            {
                                controller = "Online",
                                action = "Login"
                            }));
                        }

                    }
                }
                if (!lstActionNamNotCheck.Contains(filterContext.ActionDescriptor.ActionName))
                {
                    if (!CheckTimeOut())
                    {
                        filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
                        {
                            controller = "Online",
                            action = "Login"
                        }));

                    }
                }
                else
                {
                    if (!lstAdmin.Contains(filterContext.ActionDescriptor.ActionName))
                    {
                        base.OnActionExecuting(filterContext);
                    }
                }
            }
        }
        private const string DEFAULT_CODE = "-1";
        private const string LEVEL_DEFAULT = "1";
        private const string MESSAGE = "MESSAGE";
        private const string DISTRICT_CODE = "DISTRICT_CODE";
        #region Private Function
        public class SearchHouseHold
        {
            public string HOUSEHOLD_CODE { get; set; }
            public int? HOUSEHOLD_NUMBER { get; set; }
            public string HOUSEHOLD_NAME { get; set; }
            public string AREA_ID { get; set; }
            public string ADDRESS_ID { get; set; }
            public int? NUM_OF_HH_MEMBER { get; set; }
            public string SURVEY_METHOD_ID { get; set; }
            public string ADM_UNIT_ID { get; set; }

        }
        [AjaxValidateAntiForgeryToken]
        public JsonResult GetAdministrativeUnit3(string code, string level)
        {
            try
            {
                List<AdministrativeUnitModel> areaList = new List<AdministrativeUnitModel>();

                using (WebClient wc = new WebClient())
                {
                    Helper.SetHeaderInfo(wc, "GetByIdParentTablet");
                    wc.Encoding = Encoding.UTF8;
                    List<SelectListItem> lstItems = new List<SelectListItem>();
                    string UrlPath = string.Format("{0}{1}{2}/{3}", serviceUrl, "AdminUnitService.svc/GetByIdParentTablet/", code, level);
                    string areaDownload = wc.DownloadString(UrlPath);

                    areaList = JsonConvert.DeserializeObjectAsync<List<AdministrativeUnitModel>>(areaDownload).Result;
                    if ("1".Equals(level))
                    {
                        lstItems.Add(new SelectListItem
                        {
                            Value = "-1",
                            Text = "-- Chọn Tỉnh --"
                        });
                    }
                    foreach (var area in areaList)
                    {
                        if (area != null)
                            lstItems.Add(new SelectListItem
                            {
                                Value = area.CODE.ToString(),
                                Text = area.ADM_NAME
                            });
                    }
                    return Json(lstItems, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }
        List<SelectListItem> GetAdministrativeUnit(string code, string level)
        {
            try
            {
                List<AdministrativeUnitModel> areaList = new List<AdministrativeUnitModel>();

                using (WebClient wc = new WebClient())
                {
                    Helper.SetHeaderInfo(wc, "GetByIdParentTablet");
                    wc.Encoding = Encoding.UTF8;
                    List<SelectListItem> lstItems = new List<SelectListItem>();
                    string UrlPath = string.Format("{0}{1}{2}/{3}", serviceUrl, "AdminUnitService.svc/GetByIdParentTablet/", code, level);
                    string areaDownload = wc.DownloadString(UrlPath);

                    areaList = JsonConvert.DeserializeObjectAsync<List<AdministrativeUnitModel>>(areaDownload).Result;
                    if ("1".Equals(level))
                    {
                        lstItems.Add(new SelectListItem
                        {
                            Value = "-1",
                            Text = "-- Chọn Tỉnh --"
                        });
                    }
                    foreach (var area in areaList)
                    {
                        if (area != null)
                            lstItems.Add(new SelectListItem
                            {
                                Value = area.CODE.ToString(),
                                Text = area.ADM_NAME
                            });
                    }
                    return lstItems;
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        List<SelectListItem> GetAdministrativeUnitGenerateCode(string code, string level)
        {
            try
            {
                List<AdministrativeUnitModel> areaList = new List<AdministrativeUnitModel>();

                using (WebClient wc = new WebClient())
                {
                    Helper.SetHeaderInfo(wc, "GetByIdParentTablet");
                    wc.Encoding = Encoding.UTF8;
                    List<SelectListItem> lstItems = new List<SelectListItem>();
                    string UrlPath = string.Format("{0}{1}{2}/{3}", serviceUrl, "AdminUnitService.svc/GetByIdParentTablet/", code, level);
                    string areaDownload = wc.DownloadString(UrlPath);

                    areaList = JsonConvert.DeserializeObjectAsync<List<AdministrativeUnitModel>>(areaDownload).Result;
                    if ("1".Equals(level))
                    {
                        lstItems.Add(new SelectListItem
                        {
                            Value = "-1",
                            Text = "-- Tất cả --"
                        });
                    }
                    foreach (var area in areaList)
                    {
                        if (area != null)
                            lstItems.Add(new SelectListItem
                            {
                                Value = area.CODE.ToString(),
                                Text = area.ADM_NAME
                            });
                    }
                    return lstItems;
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        int GenerateCodeLogin(string province_code, string district_code, string commune_code, bool IsResetAllGenerate)
        {
            try
            {
                int bReturn = 0;
                string sIsResetAllGenerate = "0";
                if (IsResetAllGenerate == true)
                {
                    sIsResetAllGenerate = "1";
                }
                //int areaList;
                if (string.IsNullOrEmpty(province_code))
                {
                    province_code = DEFAULT_CODE;
                }
                if (string.IsNullOrEmpty(district_code))
                {
                    district_code = DEFAULT_CODE;
                }
                if (string.IsNullOrEmpty(commune_code))
                {
                    commune_code = DEFAULT_CODE;
                }
                using (WebClient wc = new WebClient())
                {
                    Helper.SetHeaderInfo(wc, "GenerateLoginCode");
                    wc.Encoding = Encoding.UTF8;
                    //List<SelectListItem> lstItems = new List<SelectListItem>();
                    string UrlPath = string.Format("{0}{1}{2}/{3}/{4}/{5}", serviceUrl, "OnlineService.svc/GenerateLoginCode/", province_code, district_code, commune_code, sIsResetAllGenerate);
                    string areaDownload = wc.DownloadString(UrlPath);
                    bReturn = JsonConvert.DeserializeObjectAsync<int>(areaDownload).Result;
                    //return bReturn;
                }

                return bReturn;
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }
        #endregion
        //
        // GET: /Online/
        public ActionResult Index()
        {
            return View();
        }

        #region Generate Function
        [AjaxValidateAntiForgeryToken]
        public JsonResult GetAdministrativeUnit1(string code, string level)
        {
            try
            {
                return Json(new SelectList(GetAdministrativeUnit(code, level), "Value", "Text"));
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        [Authorize]
        //[AjaxValidateAntiForgeryToken]
        //[ValidateAntiForgeryToken]
        public ActionResult GenerateCode()
        {
            try
            {
                ViewBag.ActiveMenus = ViewBag.ActiveMenus == null ? new System.Collections.Hashtable() : ViewBag.ActiveMenus;
                ViewBag.ActiveMenus[105] = "active";
                ViewBag.ActiveMenus[114] = "active";
                ViewData[DISTRICT_CODE] = GetAdministrativeUnitGenerateCode(DEFAULT_CODE, LEVEL_DEFAULT);

                return View();
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        [HttpPost]
        [Authorize]
        [AjaxValidateAntiForgeryToken]
        //[ValidateAntiForgeryToken]
        public ActionResult GenerateCode(OnlineViewModels model)
        {
            try
            {
                //process generate cod
                string message = String.Empty;
                if (model != null)
                {
                    int iCount = GenerateCodeLogin(model.PROVINCE_CODE, model.DISTRICT_CODE, model.COMMUNE_CODE, model.IsResetAllGenerate);
                    message = string.Format("Tạo mã đăng nhập thành công cho: {0} hộ.", iCount);
                }

                return Json(message);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                ViewData[MESSAGE] = string.Format("Có lỗi xảy ra trong quá trình tạo mã đăng nhập: {0}. Mời bạn thử lại sau ít phút.", ex.Message);
                throw;
            }
            //ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
            return View(model);
        }

        #endregion End Generate Function
        #region Reset Code Login


        [Authorize]
        [AjaxValidateAntiForgeryToken]
        public ActionResult ResetCodeLoginHouseHold(string loginCode)
        {
            try
            {
                HouseHoldDetailPartialViewModel houseHold = null;
                ResetCodeLoginViewModel model = new ResetCodeLoginViewModel();
                using (WebClient wc = new WebClient())
                {
                    Helper.SetHeaderInfo(wc, "GetHouseDetailByLoginCode");
                    wc.Encoding = Encoding.UTF8;
                    //List<SelectListItem> lstItems = new List<SelectListItem>();
                    string UrlPath = string.Format("{0}{1}{2}/{3}", serviceUrl, "OnlineService.svc/GetHouseDetailByLoginCode/", loginCode, "-1");
                    string result = wc.DownloadString(UrlPath);
                    houseHold = JsonConvert.DeserializeObjectAsync<HouseHoldDetailPartialViewModel>(result).Result;

                }
                if (loginCode != null)
                {
                    model.CodeLoginStart = loginCode.Substring(0, 5);
                    model.CodeLoginEnd = loginCode.Substring(5, 5);
                }
                model.Message = "Không tìm thấy hộ có mã đăng nhập: " + loginCode;
                if (houseHold == null)
                {
                    //ViewBag.Message = "Không tìm thấy hộ có mã đăng nhập phù hợp.";
                    return PartialView("~/Views/Online/ResetCodeLoginPartial.cshtml", model);
                }
                else
                {
                    if (string.IsNullOrEmpty(houseHold.HOUSEHOLD_ID))
                    {
                        ViewBag.Message = "Không tìm thấy hộ có mã đăng nhập phù hợp.";
                        return PartialView("~/Views/Online/ResetCodeLoginPartial.cshtml", model);
                    }
                    return PartialView("~/Views/Online/HouseHoldDetailPartial.cshtml", houseHold);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }
        [HttpPost]
        [Authorize]
        [AjaxValidateAntiForgeryToken]
        public ActionResult ResetCodeLoginHouseHold2(string houseHoldId)
        {
            try
            {
                string message = string.Empty;
                ResetCodeLoginViewModel model = new ResetCodeLoginViewModel();
                //reset login code
                using (WebClient wc = new WebClient())
                {
                    Helper.SetHeaderInfo(wc, "ResetLoginCodeHouseHold");
                    wc.Encoding = Encoding.UTF8;
                    //List<SelectListItem> lstItems = new List<SelectListItem>();
                    string UrlPath = string.Format("{0}{1}{2}", serviceUrl, "OnlineService.svc/ResetLoginCodeHouseHold/", houseHoldId);

                     wc.DownloadString(UrlPath);



                }
                LogStream.Write(MyUser.UserName,
                    CommonData.AppName,
                    GetFunction(),
                    CommonData.GetListId(new string[] { houseHoldId }),
                    "Reset mã đăng nhập - ResetCodeLoginHouseHold2");

                model.Message = "Reset thành công mã đăng nhập của hộ.";
                return PartialView("~/Views/Online/ResetCodeLoginPartial.cshtml", model);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        [Authorize]
        public ActionResult ResetCodeLogin()
        {
            ViewBag.ActiveMenus = ViewBag.ActiveMenus == null ? new System.Collections.Hashtable() : ViewBag.ActiveMenus;
            ViewBag.ActiveMenus[105] = "active";
            ViewBag.ActiveMenus[116] = "active";
            ResetCodeLoginViewModel model = new ResetCodeLoginViewModel();
            return View(model);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult ResetCodeLogin(ResetCodeLoginViewModel model)
        {
            //ResetCodeLoginViewModel model 
            try
            {

                if (model == null)
                {
                    model = new ResetCodeLoginViewModel();
                }
                else if (ModelState.IsValid)
                {
                    using (WebClient wc = new WebClient())
                    {
                        Helper.SetHeaderInfo(wc, "GetHouseDetailByLoginCode");
                        wc.Encoding = Encoding.UTF8;

                        //List<SelectListItem> lstItems = new List<SelectListItem>();
                        string UrlPath = string.Format("{0}{1}{2}", serviceUrl, "OnlineService.svc/GetHouseDetailByLoginCode/", string.Format("{0}{1}", model.CodeLoginStart, model.CodeLoginEnd));
                        wc.DownloadString(UrlPath);


                    }
                }
                return View(model);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        #endregion End Reset Code Login

        #region Dieu tra online online

        public ActionResult GetThongTinChung(string logincode)
        {
            try
            {
                return PartialView("~/Views/Online/GetThongTinChungPartial.cshtml");
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }
        public ActionResult GetThongTinCaNhan(string logincode)
        {
            try
            {
                return PartialView("~/Views/Online/GetThongTinCaNhanPartial.cshtml");
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }
        #region Test dieu tra

        private KhaiOnlineViewModel GetResidentByLoginCode(string ResidentId)
        {
            KhaiOnlineViewModel model = new KhaiOnlineViewModel();
            model.HOUSEHOLD_ID = 1;
            model.Next = 1;
            model.StepCurent = 0;

            return model;

        }


        public ActionResult LoginKhaiOnline(string IdLoginCode)
        {
            LoginKhaiOnlineViewModel model = new LoginKhaiOnlineViewModel();
            if (string.IsNullOrEmpty(IdLoginCode))
            {
                return View(model);
            }
            else
            {
                return PartialView("LoginKhaiOnlinePartial");
            }
        }
        [HttpGet]
        [AjaxValidateAntiForgeryToken]
        public ActionResult Pin(string IdLoginCode, string pin)
        {
            HouseHoldDetailPartialViewModel model = new HouseHoldDetailPartialViewModel();

            using (WebClient wc = new WebClient())
            {
                Helper.SetHeaderInfo(wc, "GetHouseDetailByLoginCode");
                wc.Encoding = Encoding.UTF8;

                //List<SelectListItem> lstItems = new List<SelectListItem>();
                string UrlPath = string.Format("{0}{1}{2}/{3}", serviceUrl, "OnlineService.svc/GetHouseDetailByLoginCode/", IdLoginCode, pin);
                string areaDownload = wc.DownloadString(UrlPath);
                model = JsonConvert.DeserializeObjectAsync<HouseHoldDetailPartialViewModel>(areaDownload).Result;
                //check ma pin
                if (pin == model.LOGINPIN)
                {
                    return PartialView("~/Views/Online/HouseKhaiOnlinePartial.cshtml", model);
                }
                else if ("-1".Equals(pin))
                {
                    return PartialView("~/Views/Online/HouseKhaiOnlinePartial.cshtml", model);
                }
                else
                {
                    LoginKhaiOnlineViewModel login = GetLoginKhaiOnlineViewModel(IdLoginCode);
                    login.Message = "Mã PIN nhập chưa đúng.";
                    return PartialView("~/Views/Online/PinPartial.cshtml", login);
                }

            }
            //return bReturn;


        }

        [HttpGet]
        [AjaxValidateAntiForgeryToken]
        public ActionResult HouseKhaiOnline(string IdHoused)
        {

            HouseHoldDetailPartialViewModel model = new HouseHoldDetailPartialViewModel();
            using (WebClient wc = new WebClient())
            {
                Helper.SetHeaderInfo(wc, "GetHouseDetailByLoginCode");
                wc.Encoding = Encoding.UTF8;

                //List<SelectListItem> lstItems = new List<SelectListItem>();
                string UrlPath = string.Format("{0}{1}{2}/{3}", serviceUrl, "OnlineService.svc/GetHouseDetailByLoginCode/", IdHoused, "111");
                string areaDownload = wc.DownloadString(UrlPath);
                model = JsonConvert.DeserializeObjectAsync<HouseHoldDetailPartialViewModel>(areaDownload).Result;
                //check ma pin             
                return PartialView("~/Views/Online/HouseKhaiOnlinePartial.cshtml", model);

            }
        }

        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult ResidentListBack(string HOUSEHOLD_ID, string IsBack)
        {
            if (IsBack == "2")
            {
                LoginKhaiOnlineViewModel lg = new LoginKhaiOnlineViewModel();
                return PartialView("~/Views/Online/LoginKhaiOnlinePartial.cshtml", lg);
            }
            else
            {
                using (WebClient wc = new WebClient())
                {
                    Helper.SetHeaderInfo(wc, "GetbyRESIDENTbyHOUSEHOLD_ID");
                    wc.Encoding = Encoding.UTF8;
                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetbyRESIDENTbyHOUSEHOLD_ID");
                    string json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(HOUSEHOLD_ID);
                    string result = wc.UploadString(UrlPath, "POST", json);
                    List<Resident1> lst = JsonConvert.DeserializeObjectAsync<List<Resident1>>(result).Result;
                    return PartialView("ResidentAddOrSavePartial", lst);
                }
            }

        }

        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult HouseKhaiOnline(HouseHoldDetailPartialViewModel model)
        {
            //save data house
            if (ModelState.IsValid)
            {
                //get data list President 1

                using (WebClient wc = new WebClient())
                {
                    wc.Encoding = Encoding.UTF8;
                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                    string UrlPath = string.Empty;
                    string result = string.Empty;
                    string json = string.Empty;
                    //process next
                    if (model != null)
                    {
                        if (model.IsBack == "2")
                        {
                            Helper.SetHeaderInfo(wc, "OnlineSaveAndGetResident");
                            UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/OnlineSaveAndGetResident");

                            json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                            result = wc.UploadString(UrlPath, "POST", json);
                            List<Resident1> lst = JsonConvert.DeserializeObjectAsync<List<Resident1>>(result).Result;
                            int iCountResident = lst.Count();
                            int numOfMember = (model.NUM_OF_HH_MEMBER ?? 0) - iCountResident;

                            if (numOfMember > 0)
                            {
                                for (int i = 0; i < numOfMember; i++)
                                {
                                    Resident1 resident = new Resident1()
                                    {
                                        HOUSEHOLD_ID = model.HOUSEHOLD_ID

                                    };
                                    if (i == 0 && lst.Count().Equals(0) && !string.IsNullOrEmpty(model.HOUSEHOLD_NAME))
                                    {

                                        string[] array = model.HOUSEHOLD_NAME.Split(' ');
                                        if (array.Length > 0)
                                        {
                                            resident.LAST_NAME = array[0];
                                        }
                                        if (array.Length >= 2)
                                        {
                                            resident.FIRST_NAME = array[array.Length - 1];
                                        }
                                        if (array.Length >= 3)
                                        {
                                            for (int j = 1; j < array.Length - 1; j++)
                                            {
                                                if (j != 1)
                                                {
                                                    resident.MIDDLE_NAME = string.Format("{0} {1}", resident.MIDDLE_NAME, array[j]);
                                                }
                                            }
                                            //resident.MIDDLE_NAME = resident.MIDDLE_NAME.Substring(1);
                                        }


                                    }


                                    lst.Add(resident);
                                }
                            }
                        }
                        LoginKhaiOnlineViewModel lg = new LoginKhaiOnlineViewModel();
                        return PartialView("~/Views/Online/LoginKhaiOnlinePartial.cshtml", lg);
                    }
                    //process back
                    else
                    {
                        Helper.SetHeaderInfo(wc, "OnlineSaveAndGetResident");
                        UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/OnlineSaveAndGetResident");
                        json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                        result = wc.UploadString(UrlPath, "POST", json);
                        List<Resident1> lst = JsonConvert.DeserializeObjectAsync<List<Resident1>>(result).Result;
                        int iCountResident = lst.Count();
                        int numOfMember = NUMBER_OF_HOUSEHOLD_DEFAULT - iCountResident;

                        if (numOfMember > 0)
                        {
                            for (int i = 0; i < numOfMember; i++)
                            {
                                Resident1 resident = new Resident1()
                                {
                                    HOUSEHOLD_ID = model.HOUSEHOLD_ID

                                };
                                if (i == 0 && lst.Count().Equals(0))
                                {
                                    resident.RELATION_ID = "1";
                                    if (!string.IsNullOrEmpty(model.HOUSEHOLD_NAME))
                                    {
                                        string[] array = model.HOUSEHOLD_NAME.Split(' ');
                                        if (array.Length >= 1)
                                        {
                                            resident.LAST_NAME = array[0];
                                        }
                                        if (array.Length >= 2)
                                        {
                                            resident.FIRST_NAME = array[array.Length - 1];
                                        }
                                        if (array.Length >= 3)
                                        {
                                            for (int j = 1; j < array.Length - 1; j++)
                                            {
                                                if (j == 1)
                                                {
                                                    resident.MIDDLE_NAME = array[j];
                                                }
                                                else
                                                {
                                                    resident.MIDDLE_NAME = resident.MIDDLE_NAME + " " + array[j];
                                                }
                                            }
                                            //resident.MIDDLE_NAME = model.HOUSEHOLD_NAME.Substring(resident.LAST_NAME.Length);
                                            //resident.MIDDLE_NAME = resident.MIDDLE_NAME.Substring(0, resident.MIDDLE_NAME.Length - resident.LAST_NAME.Length);
                                            //resident.MIDDLE_NAME = resident.MIDDLE_NAME.Substring(1);
                                        }
                                    }

                                }

                                lst.Add(resident);
                            }
                        }
                        return PartialView("ResidentAddOrSavePartial", lst);

                        //UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/OnlineSaveAndGetHouseHold");


                        //return View();
                    }


                }


            }
            else
            {
                return PartialView("~/Views/Online/HouseKhaiOnlinePartial.cshtml", model);
            }

        }

        LoginKhaiOnlineViewModel GetLoginKhaiOnlineViewModel(string loginCode)
        {
            LoginKhaiOnlineViewModel model = new LoginKhaiOnlineViewModel();
            using (WebClient wc = new WebClient())
            {
                Helper.SetHeaderInfo(wc, "GetLoginKhaiOnline");
                wc.Encoding = Encoding.UTF8;
                //List<ResidentDetailViewModel> residentList = null;
                //List<SelectListItem> lstItems = new List<SelectListItem>();
                string UrlPath = string.Format("{0}{1}{2}", serviceUrl, "OnlineService.svc/GetLoginKhaiOnline/", loginCode);
                string areaDownload = wc.DownloadString(UrlPath);
                model = JsonConvert.DeserializeObjectAsync<LoginKhaiOnlineViewModel>(areaDownload).Result;
                //model.HouseDetail = houseDetail;
                return model;
                //return bReturn;
            }
        }
        [HttpPost]
        [ValidateAntiForgeryToken()]
        [AllowAnonymous]
        [CaptchaValidation("CaptchaCode", "PM0Captcha", "Bạn chưa nhập Mã bảo vệ hoặc Mã bảo vệ không đúng!")] //PM0Captcha là tên đối tượng captcha trong file cshtml, CapthcaCode là tên control người dùng nhập mã Capthca
        public ActionResult LoginWithCaptcha(LoginKhaiOnlineViewModel user)
        {
            return LoginKhaiOnline(user);
            //return LoginKhaiOnline(user);
        }
        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult ChangeStepSurvey(StepSurveyModel model)
        {
            if (model != null)
            {
                switch (model.ActionName)
                {
                    case "Khaisinh1":
                        Khaisinh1Model khaisinh1 = GetKhaiSinh1Model(model.ResidentId);
                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                        return PartialView("Khaisinh1Partial", khaisinh1);
                    case "Khaisinh2":
                        Khaisinh2Model khaisinh2 = GetKhaiSinh2Model(model.ResidentId);
                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                        return PartialView("Khaisinh2Partial", khaisinh2);
                    case "Khaisinh3":
                        Khaisinh3Model khaisinh3 = GetKhaiSinh3Model(model.ResidentId);
                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                        return PartialView("Khaisinh3Partial", khaisinh3);
                    default:
                        //mac dinh
                        Khaisinh2Model khaisinhDefault = new Khaisinh2Model();
                        khaisinhDefault.Next = "99";
                        khaisinhDefault.RESIDENT_ID = model.ResidentId;
                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                        return PartialView("Khaisinh1Partial", khaisinhDefault);
                    //break;
                }
            }
            else
            {
                return Json(String.Empty);
            }
            //return View();
        }
        #region Get data khai sinh
        private Khaisinh1Model GetKhaiSinh1Model(string residentId)
        {
            Khaisinh1Model khaisinh1 = new Khaisinh1Model();
            try
            {
                Khaisinh2Model khaisinh2 = new Khaisinh2Model();
                khaisinh2.Next = "99";
                khaisinh2.RESIDENT_ID = residentId;
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "UpdateKhaisinh2Back");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateKhaisinh2Back");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(khaisinh2);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        khaisinh1 = JsonConvert.DeserializeObjectAsync<Khaisinh1Model>(result).Result;
                    }

                }
                return khaisinh1;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                throw;
            }
        }
        private Khaisinh2Model GetKhaiSinh2Model(string residentId)
        {
            Khaisinh2Model khaisinh2 = new Khaisinh2Model();
            Khaisinh3Model khaisinh3 = new Khaisinh3Model();
            khaisinh3.RESIDENT_ID = residentId;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "UpdateKhaisinh3Back");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateKhaisinh3Back");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(khaisinh3);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        khaisinh2 = JsonConvert.DeserializeObjectAsync<Khaisinh2Model>(result).Result;
                    }

                }
                return khaisinh2;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                throw;
            }
            //ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
        }
        private Khaisinh3Model GetKhaiSinh3Model(string residentId)
        {
            Khaisinh3Model khaisinh3 = new Khaisinh3Model();
            Khaisinh4Model khaisinh4 = new Khaisinh4Model();
            khaisinh4.RESIDENT_ID = residentId;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "UpdateKhaisinh4Back");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateKhaisinh4Back");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(khaisinh4);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        khaisinh3 = JsonConvert.DeserializeObjectAsync<Khaisinh3Model>(result).Result;
                    }

                }
                return khaisinh3;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                throw;
            }
        }
        #endregion


        #region Region Login Khai Online
        public ActionResult Login()
        {
            return View();
        }
        public ActionResult LogOff()
        {
            Session.Clear();
            Session.RemoveAll();
            Session.Abandon();
            if (Request.Cookies["ASP.NET_SessionId"] != null)
            {
                Response.Cookies["ASP.NET_SessionId"].Value = string.Empty;
                Response.Cookies["ASP.NET_SessionId"].Expires = DateTime.Now.AddMonths(-20);
            }

            if (Request.Cookies["AuthToken"] != null)
            {
                Response.Cookies["AuthToken"].Value = string.Empty;
                Response.Cookies["AuthToken"].Expires = DateTime.Now.AddMonths(-20);
            }
            return RedirectToAction("Login", "Online");
        }

        public ActionResult Home()
        {

            return View();
        }

        public ActionResult Resident()
        {
            //get data quan he chu ho      
            ViewData["relation"] = GetMasterListByName("RELATION");
            //get gioi tinh
            ViewData["gender"] = GetMasterListByName("GENDER");
            //get dan toc
            ViewData["ethnic"] = GetMasterListByName("ETHNIC");
            return View();
        }
        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public void ResidentPost()
        {

            Hashtable result = new Hashtable();
            string json = string.Empty;
            //get data post
            string[] arrayRelation = Request.Form.GetValues("relation_id");
            string[] arrayLastName = Request.Form.GetValues("last_name");
            string[] arrayMiddleName = Request.Form.GetValues("middle_name");
            string[] arrayFirstName = Request.Form.GetValues("first_name");
            string[] arrayGender = Request.Form.GetValues("gender_id");
            string[] arrayEthnic = Request.Form.GetValues("ethnic_id");
            string[] arrayResidentId = Request.Form.GetValues("resident_id");
            IList<Resident1> lst = new List<Resident1>();
            bool bReturn = true;
            for (int i = 0; i < arrayRelation.Length; i++)
            {

                //validata data
                if (!(string.IsNullOrEmpty(arrayLastName[i]) || string.IsNullOrEmpty(arrayFirstName[i]) || string.IsNullOrEmpty(arrayGender[i]) || string.IsNullOrEmpty(arrayEthnic[i])))
                {
                    //insert resident
                    Resident1 resident = new Resident1()
                    {
                        RESIDENT_ID = string.IsNullOrEmpty(arrayResidentId[i]) ? null : arrayResidentId[i],
                        HOUSEHOLD_ID = MySessionUser.HouseholdId,
                        RELATION_ID = string.IsNullOrEmpty(arrayRelation[i]) ? null : arrayRelation[i],
                        LAST_NAME = arrayLastName[i],
                        MIDDLE_NAME = arrayMiddleName[i],
                        FIRST_NAME = arrayFirstName[i],
                        GENDER_ID = arrayGender[i],
                        ETHNIC_ID = arrayEthnic[i]
                    };
                    lst.Add(resident);
                }
                else
                {
                    bReturn = false;
                }
            }
            if (bReturn)
            {
                json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(lst);
                //insert into database
                if (InsertResidentList(json))
                {
                    result["success"] = true;
                    result["msg"] = "sucessfull";
                }
                else
                {
                    result["success"] = false;
                    result["msg"] = "Có lỗi xảy ra trong quá trình lưu dữ liệu.";
                }
            }
            else
            {
                result["success"] = false;
                result["msg"] = "Có lỗi xảy ra trong quá trình lưu dữ liệu.";
            }
            json = Newtonsoft.Json.JsonConvert.SerializeObject(result);
            Response.Write(json);
        }

        private bool InsertResidentList(string json)
        {
            using (WebClient wc = new WebClient())
            {
                Helper.SetHeaderInfo(wc, "InsertOrUpdateResident2");
                wc.Encoding = Encoding.UTF8;
                wc.Headers["Content-type"] = "application/json; charset=utf-8";
                try
                {
                    string urlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/InsertOrUpdateResident2");

                    wc.UploadString(urlPath, "POST", json);
                    return true;
                }
                catch (Exception ex)
                {
                    logger.Error(ex);                    
                    return false;
                }
            }
        }
        [HttpGet]
        [AjaxValidateAntiForgeryToken]
        public JsonResult GetResidentById()
        {
            if (!string.IsNullOrEmpty(MySessionUser.HouseholdId))
            {
                return Json(GetResidentByHouseHoldId(MySessionUser.HouseholdId), JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new List<Resident1>(), JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult ResidentDetail()
        {
            return View();
        }

        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public void HomePost()
        {
            Hashtable result = new Hashtable();
            HouseHoldDetailPartialViewModel household = new HouseHoldDetailPartialViewModel();
            //get data household
            household.HOUSEHOLD_NAME = Request.Form["HOUSEHOLD_NAME"];
            household.HH_HEAD_EMAIL = Request.Form["HH_HEAD_EMAIL"];
            household.HH_HEAD_MOBILE = Request.Form["HH_HEAD_MOBILE"];
            household.ADDRESS_NAME = Request.Form["ADDRESS_NAME"];
            household.HOUSEHOLD_ID = MySessionUser.HouseholdId;
            //validata data
            //get and check data from client
            string json = string.Empty;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    Helper.SetHeaderInfo(wc, "OnlineSaveHousehold");
                    wc.Encoding = Encoding.UTF8;
                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/OnlineSaveHousehold");
                    json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(household);
                    json = wc.UploadString(UrlPath, "POST", json);
                    result["msg"] = json;
                }
                result["success"] = true;
                json = Newtonsoft.Json.JsonConvert.SerializeObject(result);
                Response.Write(json);
            }
            catch (Exception ex)
            {
                result["success"] = false;
                result["msg"] = ex.Message;
                json = Newtonsoft.Json.JsonConvert.SerializeObject(result);
                Response.Write(json);
            }
        }
        [HttpGet]
        [AjaxValidateAntiForgeryToken]
        public void GetHouseholdDetail()
        {
            HouseHoldDetailPartialViewModel household = new HouseHoldDetailPartialViewModel();
            using (WebClient wc = new WebClient())
            {
                Helper.SetHeaderInfo(wc, "GetHouseDetailByLoginCode");
                wc.Encoding = Encoding.UTF8;
                SessionUsers user = MySessionUser;
                //get household
                string UrlPath = string.Format("{0}{1}{2}/{3}", serviceUrl, "OnlineService.svc/GetHouseDetailByLoginCode/", user.LoginCode, "111");
                string result = wc.DownloadString(UrlPath);
                household = JsonConvert.DeserializeObjectAsync<HouseHoldDetailPartialViewModel>(result).Result;
                //check ma pin        
                //user.HouseholdId = user.HouseholdId;
                MySessionUser = user;
                Response.Write(result);

            }
        }

        [AjaxValidateAntiForgeryToken]
        [HttpPost]
        public void GetLoginCountSessionCount()
        {
            Hashtable result = new Hashtable();
            result["login_count"] = MySessionUser.LoginCount;
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(result);
            Response.Write(json);
        }
        //private SessionUsers _mySessionUser;
        public SessionUsers MySessionUser
        {
            get
            {
                if (HttpContext.Session["session_user"] == null)
                {
                    HttpContext.Session["session_user"] = new SessionUsers();
                }
                return HttpContext.Session["session_user"] as SessionUsers;
            }
            set
            {
                HttpContext.Application["session_user"] = value;
            }
        }

        [AjaxValidateAntiForgeryToken]
        [HttpPost]
        public void LoginPost()
        {
            string start = Request.Form["Start"];
            string end = Request.Form["End"];
            Hashtable result = new Hashtable();

            SessionUsers sessionUser = MySessionUser;
            //get data login
            LoginKhaiOnlineViewModel loginCheck = GetLoginKhaiOnlineViewModel(string.Format("{0}{1}", start, end));
            //check login code
            if (!string.IsNullOrEmpty(loginCheck.LoginCode))
            {
                //check capchar
                if (sessionUser.LoginCount > 3)
                {
                    bool ajaxValidationResult = CaptchaControl.AjaxValidate("pm0Captcha", Request.Form["CaptchaCode"], Request.Form["LBD_VCID_PM0Captcha"]);
                    if (ajaxValidationResult)
                    {
                        string guid = Guid.NewGuid().ToString();
                        Session["AuthToken"] = guid;
                        Response.Cookies.Add(new HttpCookie("AuthToken", guid));
                        result["success"] = true;
                        sessionUser.LoginCount = 0;
                        sessionUser.LoginCode = loginCheck.LoginCode;
                        result["msg"] = "Đăng nhập thành công";
                    }
                    else
                    {
                        sessionUser.LoginCount += 1;
                        result["msg"] = "Thông mã bảo mật không đúng";
                        result["success"] = false;
                    }
                }
                else
                {
                    string guid = Guid.NewGuid().ToString();
                    Session["AuthToken"] = guid;
                    Response.Cookies.Add(new HttpCookie("AuthToken", guid));
                    result["success"] = true;
                    sessionUser.LoginCount = 0;
                    sessionUser.LoginCode = loginCheck.LoginCode;
                    result["msg"] = "Đăng nhập thành công";
                }


            }
            else
            {
                sessionUser.LoginCount += 1;
                result["msg"] = "Thông tin đăng nhập không đúng";
                result["success"] = false;
            }

            //reset count login
            result["login_count"] = sessionUser.LoginCount;
            if ((bool)result["success"] && 1.Equals(loginCheck.IsLoginFirst))
            {
                result["pin"] = loginCheck.PIN;
            }
            else
            {
                result["pin"] = string.Empty;
            }
            //return success
            //set session equal Session User
            MySessionUser = sessionUser;
            //write             
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(result);
            Response.Write(json);
        }

        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public void PinPost()
        {
            Hashtable result = new Hashtable();
            string pin = Request.Form["Pin"];
            if (string.IsNullOrEmpty(pin))
            {
                pin = "-1";
            }
            SessionUsers mysessionuser = MySessionUser;
            string logincode = mysessionuser.LoginCode;
            HouseHoldDetailPartialViewModel model = new HouseHoldDetailPartialViewModel();
            using (WebClient wc = new WebClient())
            {
                wc.Encoding = Encoding.UTF8;
                Helper.SetHeaderInfo(wc, "GetHouseDetailByLoginCode");
                //List<SelectListItem> lstItems = new List<SelectListItem>();
                string UrlPath = string.Format("{0}{1}{2}/{3}", serviceUrl, "OnlineService.svc/GetHouseDetailByLoginCode/", logincode, pin);
                string areaDownload = wc.DownloadString(UrlPath);
                model = JsonConvert.DeserializeObjectAsync<HouseHoldDetailPartialViewModel>(areaDownload).Result;
                mysessionuser.HouseholdId = model.HOUSEHOLD_ID;
                MySessionUser = mysessionuser;
                //check ma pin dung
                if (pin == model.LOGINPIN)
                {
                    //true
                    Session["UserInfoOnline"] = model.HOUSEHOLD_ID;
                    result["success"] = true;
                    result["msg"] = "Đăng nhập thành công";


                }//ma pin sai
                else
                {
                    result["success"] = false;
                    result["msg"] = "Thông tin mã PIN không đúng";
                }

            }

            string json = Newtonsoft.Json.JsonConvert.SerializeObject(result);
            Response.Write(json);
        }


        #endregion End Region Login Khai Online
        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        //[OutputCache]
        //[CaptchaValidation("CaptchaCode", "PM0Captcha", "Bạn chưa nhập Mã bảo vệ hoặc Mã bảo vệ không đúng!")] 
        public ActionResult LoginKhaiOnline(LoginKhaiOnlineViewModel model)
        {

            try
            {
                LoginKhaiOnlineViewModel login1 = new LoginKhaiOnlineViewModel();
                if (ModelState.IsValid)
                {
                    if (model != null)
                    {
                        login1 = GetLoginKhaiOnlineViewModel(model.LoginCode);
                        login1.LoginTime = model.LoginTime + 1;
                    }

                    bool ajaxValidationResult = CaptchaControl.AjaxValidate(model.CaptchaId, model.UserInput, model.InstanceId);
                    //chua can nhap ma capcha
                    if (login1.LoginTime > 3 && !ajaxValidationResult)
                    {

                        login1.Message = "Mã xác thực không đúng.";
                        //return LoginWithCaptcha(login1);
                        return PartialView("LoginKhaiOnlinePartial", login1);


                    }
                    if (!string.IsNullOrEmpty(login1.LoginCode))
                    {

                        return PartialView("~/Views/Online/PinPartial.cshtml", login1);

                    }
                    else
                    {
                        login1.Message = "Mã đăng nhập không đúng.";
                        //return LoginWithCaptcha(login1);
                        return PartialView("LoginKhaiOnlinePartial", login1);
                    }

                }
                else
                {
                    login1.Message = "Mã đăng nhập không đúng.";
                    //return LoginWithCaptcha(login1);
                    return PartialView("LoginKhaiOnlinePartial", login1);
                }


            }
            catch (Exception ex)
            {
                logger.Error(ex);
                throw;
            }

        }

        private List<DHQGHN.PM2.Models.Online.Resident1> GetResidentByHouseHoldId(string household_id)
        {
            try
            {
                List<DHQGHN.PM2.Models.Online.Resident1> lst = new List<Resident1>();
                using (WebClient wc = new WebClient())
                {
                    Helper.SetHeaderInfo(wc, "GetbyRESIDENTbyHOUSEHOLD_ID");
                    wc.Encoding = Encoding.UTF8;
                    string UrlPath = string.Format("{0}{1}/{2}", serviceUrl, "OnlineService.svc/GetbyRESIDENTbyHOUSEHOLD_ID/", household_id);
                    string result = wc.DownloadString(UrlPath);
                    lst = JsonConvert.DeserializeObjectAsync<List<Resident1>>(result).Result;
                    return lst;
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                throw;
            }
        }
        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        //List<DHQGHN.PM2.Models.Online.Resident1> lst
        public ActionResult ResidentList(List<DHQGHN.PM2.Models.Online.Resident1> lst)
        {
            try
            {
                //lst = new List<Resident1>();
                //lst.Add(new Resident1() { RESIDENT_ID = "1" });
                //save data
                string Url = string.Empty;
                var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(lst);
                string jsonResult = string.Empty;
                string UrlPath = string.Empty;
                //savdata Resident1
                //get data thong tin chung tra ve list Resident1
                if (ModelState.IsValid)
                {
                    using (WebClient wc = new WebClient())
                    {
                        wc.Encoding = Encoding.UTF8;
                        
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        if (lst != null)
                        {
                            if (lst[0].IsBack == "1")
                            {

                                //List<SelectListItem> lstItems = new List<SelectListItem>();
                                Helper.SetHeaderInfo(wc, "InsertOrUpdateResident1");
                                UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/InsertOrUpdateResident1");

                                jsonResult = wc.UploadString(UrlPath, "POST", json);
                                List<ResidentDetailViewModel> residentList = JsonConvert.DeserializeObjectAsync<List<ResidentDetailViewModel>>(jsonResult).Result;

                                lst = new List<Resident1>();
                                foreach (var item in residentList)
                                {
                                    Resident1 obj = new Resident1()
                                    {
                                        RESIDENT_ID = item.RESIDENT_ID,
                                        HOUSEHOLD_ID = item.HOUSEHOLD_ID,
                                        FIRST_NAME = item.FIRST_NAME,
                                        LAST_NAME = item.LAST_NAME,
                                        MIDDLE_NAME = item.MIDDLE_NAME,
                                        FULL_NAME = item.FULL_NAME
                                    };
                                    lst.Add(obj);

                                }

                                return PartialView("~/Views/Online/KhaiOnlinePartial.cshtml", lst);
                                //return bReturn;
                            }
                            //process back
                            //return household detail
                            else
                            {
                                if (lst[0].IsBack == "2")
                                {
                                    LoginKhaiOnlineViewModel lg = new LoginKhaiOnlineViewModel();
                                    //List<SelectListItem> lstItems = new List<SelectListItem>();
                                    Helper.SetHeaderInfo(wc, "InsertOrUpdateResident1");
                                    UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/InsertOrUpdateResident1");

                                    jsonResult = wc.UploadString(UrlPath, "POST", json);
                                    List<ResidentDetailViewModel> residentList = JsonConvert.DeserializeObjectAsync<List<ResidentDetailViewModel>>(jsonResult).Result;

                                    lst = new List<Resident1>();
                                    foreach (var item in residentList)
                                    {

                                        Resident1 obj = new Resident1()
                                        {
                                            RESIDENT_ID = item.RESIDENT_ID,
                                            HOUSEHOLD_ID = item.HOUSEHOLD_ID,
                                            FIRST_NAME = item.FIRST_NAME,
                                            LAST_NAME = item.LAST_NAME,
                                            MIDDLE_NAME = item.MIDDLE_NAME,
                                            FULL_NAME = item.FULL_NAME
                                        };
                                        lst.Add(obj);

                                    }
                                    return PartialView("~/Views/Online/LoginKhaiOnlinePartial.cshtml", lg);
                                }
                                else
                                {
                                    Helper.SetHeaderInfo(wc, "InsertOrUpdateResidentAndGetHouseHold");
                                    UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/InsertOrUpdateResidentAndGetHouseHold");
                                    jsonResult = wc.UploadString(UrlPath, "POST", json);
                                    HouseHoldDetailPartialViewModel houseHold = JsonConvert.DeserializeObjectAsync<HouseHoldDetailPartialViewModel>(jsonResult).Result;
                                    return PartialView("~/Views/Online/HouseKhaiOnlinePartial.cshtml", houseHold);
                                }
                            }
                        }
                        else
                        {
                            return Json(String.Empty);
                        }
                    }
                }
                else
                {
                    Url = "~/Views/Online/ResidentListPartial.cshtml";
                    return PartialView("ResidentAddOrSavePartial", lst);
                }

            }
            catch (Exception ex)
            {
                logger.Error(ex);
                throw;
            }
        }
        [HttpGet]
        [AjaxValidateAntiForgeryToken]
        public ActionResult KhaiOnline(string residentId)
        {
            string Url = "~/Views/Online/ThongTinCaNhanPartial.cshtml";
            ThongTinCaNhanModel ttcn = new ThongTinCaNhanModel();
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "GetThongtincanhan");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetThongtincanhan");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(residentId);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        ttcn = JsonConvert.DeserializeObjectAsync<ThongTinCaNhanModel>(result).Result;
                    }

                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                throw;
            }
            ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
            return PartialView(Url, ttcn);
        }

        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult TrinhTrangHonNhan1(TinhTrangHonNhanModel tthn1)
        {
            string Url = "~/Views/Online/TinhTrangHonNhan2Partial.cshtml";

            return PartialView(Url);
        }
        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult Khaisinh4(Khaisinh4Model model)
        {
            if (model != null)
            {
                if (model.Next == "1")
                {
                    string Url = "~/Views/Online/TinhTrangHonNhanPartial.cshtml";
                    TinhTrangHonNhanModel tthn = new TinhTrangHonNhanModel();
                    try
                    {
                        using (WebClient wc = new WebClient())
                        {
                            if (ModelState.IsValid)
                            {
                                Helper.SetHeaderInfo(wc, "UpdateKhaisinh4");
                                wc.Encoding = Encoding.UTF8;

                                wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateKhaisinh4");
                                var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                string result = wc.UploadString(UrlPath, "POST", json);
                                tthn = JsonConvert.DeserializeObjectAsync<TinhTrangHonNhanModel>(result).Result;
                            }

                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex);
                        throw;
                    }
                    ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);

                    return PartialView(Url, tthn);
                }
                else
                {
                    if (model.Next == "2")
                    {
                        string Url = "~/Views/Online/LoginKhaiOnlinePartial.cshtml";
                        TinhTrangHonNhanModel tthn = new TinhTrangHonNhanModel();
                        LoginKhaiOnlineViewModel lg = new LoginKhaiOnlineViewModel();
                        try
                        {
                            using (WebClient wc = new WebClient())
                            {
                                if (ModelState.IsValid)
                                {
                                    Helper.SetHeaderInfo(wc, "UpdateKhaisinh4");
                                    wc.Encoding = Encoding.UTF8;
                                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateKhaisinh4");
                                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                    string result = wc.UploadString(UrlPath, "POST", json);
                                    tthn = JsonConvert.DeserializeObjectAsync<TinhTrangHonNhanModel>(result).Result;
                                }

                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex);
                            throw;
                        }
                        return PartialView(Url, lg);
                    }
                    else
                    {
                        string Url = "~/Views/Online/Khaisinh3Partial.cshtml";
                        Khaisinh3Model ks3 = new Khaisinh3Model();
                        try
                        {
                            using (WebClient wc = new WebClient())
                            {
                                if (ModelState.IsValid)
                                {
                                    Helper.SetHeaderInfo(wc, "UpdateKhaisinh4Back");
                                    wc.Encoding = Encoding.UTF8;
                                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateKhaisinh4Back");
                                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                    string result = wc.UploadString(UrlPath, "POST", json);
                                    ks3 = JsonConvert.DeserializeObjectAsync<Khaisinh3Model>(result).Result;
                                }

                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex);
                            throw;
                        }
                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);

                        return PartialView(Url, ks3);
                    }
                }
            }
            else { return Json(String.Empty); }
        }
        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult Khaisinh3(Khaisinh3Model model)
        {
            if (model != null)
            {
                if (model.Next == "1")
                {
                    string Url = "~/Views/Online/Khaisinh4Partial.cshtml";
                    Khaisinh4Model khaisinh4 = new Khaisinh4Model();
                    try
                    {
                        using (WebClient wc = new WebClient())
                        {
                            if (ModelState.IsValid)
                            {
                                Helper.SetHeaderInfo(wc, "UpdateKhaisinh3");
                                wc.Encoding = Encoding.UTF8;
                                wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateKhaisinh3");
                                var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                string result = wc.UploadString(UrlPath, "POST", json);
                                khaisinh4 = JsonConvert.DeserializeObjectAsync<Khaisinh4Model>(result).Result;
                            }

                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex);
                        throw;
                    }
                    ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                    return PartialView(Url, khaisinh4);
                }
                else
                {
                    if (model.Next == "2")
                    {
                        string Url = "~/Views/Online/LoginKhaiOnlinePartial.cshtml";
                        Khaisinh4Model khaisinh4 = new Khaisinh4Model();
                        LoginKhaiOnlineViewModel lg = new LoginKhaiOnlineViewModel();
                        try
                        {
                            using (WebClient wc = new WebClient())
                            {
                                if (ModelState.IsValid)
                                {
                                    Helper.SetHeaderInfo(wc, "UpdateKhaisinh3");
                                    wc.Encoding = Encoding.UTF8;
                                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateKhaisinh3");
                                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                    string result = wc.UploadString(UrlPath, "POST", json);
                                    khaisinh4 = JsonConvert.DeserializeObjectAsync<Khaisinh4Model>(result).Result;
                                }

                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex);
                            throw;
                        }
                        return PartialView(Url, lg);
                    }
                    else
                    {
                        string Url = "~/Views/Online/Khaisinh2Partial.cshtml";
                        Khaisinh2Model khaisinh2 = new Khaisinh2Model();
                        try
                        {
                            using (WebClient wc = new WebClient())
                            {
                                if (ModelState.IsValid)
                                {
                                    Helper.SetHeaderInfo(wc, "UpdateKhaisinh3Back");
                                    wc.Encoding = Encoding.UTF8;
                                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateKhaisinh3Back");
                                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                    string result = wc.UploadString(UrlPath, "POST", json);
                                    khaisinh2 = JsonConvert.DeserializeObjectAsync<Khaisinh2Model>(result).Result;
                                }

                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex);
                            throw;
                        }
                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                        return PartialView(Url, khaisinh2);
                    }
                }
            }
            else
            {
                return Json(String.Empty);
            }
        }
        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult Khaisinh2(Khaisinh2Model model)
        {
            if (model != null)
            {
                if (model.Next == "1")
                {
                    string Url = "~/Views/Online/Khaisinh3Partial.cshtml";
                    Khaisinh3Model khaisinh3 = new Khaisinh3Model();
                    try
                    {
                        using (WebClient wc = new WebClient())
                        {
                            if (ModelState.IsValid)
                            {
                                Helper.SetHeaderInfo(wc, "UpdateKhaisinh2");
                                wc.Encoding = Encoding.UTF8;
                                wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateKhaisinh2");
                                var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                string result = wc.UploadString(UrlPath, "POST", json);
                                khaisinh3 = JsonConvert.DeserializeObjectAsync<Khaisinh3Model>(result).Result;
                            }

                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex);
                        throw;
                    }
                    ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                    return PartialView(Url, khaisinh3);
                }
                else
                {
                    if (model.Next == "2")
                    {
                        string Url = "~/Views/Online/LoginKhaiOnlinePartial.cshtml";
                        Khaisinh3Model khaisinh3 = new Khaisinh3Model();
                        LoginKhaiOnlineViewModel lg = new LoginKhaiOnlineViewModel();
                        try
                        {
                            using (WebClient wc = new WebClient())
                            {
                                if (ModelState.IsValid)
                                {
                                    Helper.SetHeaderInfo(wc, "UpdateKhaisinh2");
                                    wc.Encoding = Encoding.UTF8;
                                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateKhaisinh2");
                                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                    string result = wc.UploadString(UrlPath, "POST", json);
                                    khaisinh3 = JsonConvert.DeserializeObjectAsync<Khaisinh3Model>(result).Result;
                                }

                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex);
                            throw;
                        }
                        return PartialView(Url, lg);
                    }
                    else
                    {
                        string Url = "~/Views/Online/Khaisinh1Partial.cshtml";
                        Khaisinh1Model khaisinh1 = new Khaisinh1Model();
                        try
                        {
                            using (WebClient wc = new WebClient())
                            {
                                if (ModelState.IsValid)
                                {
                                    Helper.SetHeaderInfo(wc, "UpdateKhaisinh2Back");
                                    wc.Encoding = Encoding.UTF8;
                                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateKhaisinh2Back");
                                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                    string result = wc.UploadString(UrlPath, "POST", json);
                                    khaisinh1 = JsonConvert.DeserializeObjectAsync<Khaisinh1Model>(result).Result;
                                }

                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex);
                            throw;
                        }
                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                        return PartialView(Url, khaisinh1);
                    }
                }
            }
            else
            {
                return Json(String.Empty);
            }

        }
        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult Khaisinh1(Khaisinh1Model model)
        {
            if (model != null)
            {
                if (model.Next == "1")
                {
                    string Url = "~/Views/Online/Khaisinh2Partial.cshtml";
                    Khaisinh2Model khaisinh2 = new Khaisinh2Model();
                    try
                    {
                        using (WebClient wc = new WebClient())
                        {
                            if (ModelState.IsValid)
                            {
                                Helper.SetHeaderInfo(wc, "UpdateKhaisinh1");
                                wc.Encoding = Encoding.UTF8;
                                wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateKhaisinh1");
                                var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                string result = wc.UploadString(UrlPath, "POST", json);
                                khaisinh2 = JsonConvert.DeserializeObjectAsync<Khaisinh2Model>(result).Result;
                            }

                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex);
                        throw;
                    }
                    ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                    return PartialView(Url, khaisinh2);
                }
                else
                {
                    if (model.Next == "2")
                    {
                        string Url = "~/Views/Online/LoginKhaiOnlinePartial.cshtml";
                        Khaisinh2Model khaisinh2 = new Khaisinh2Model();
                        LoginKhaiOnlineViewModel lg = new LoginKhaiOnlineViewModel();
                        try
                        {
                            using (WebClient wc = new WebClient())
                            {
                                if (ModelState.IsValid)
                                {
                                    Helper.SetHeaderInfo(wc, "UpdateKhaisinh1");
                                    wc.Encoding = Encoding.UTF8;
                                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateKhaisinh1");
                                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                    string result = wc.UploadString(UrlPath, "POST", json);
                                    khaisinh2 = JsonConvert.DeserializeObjectAsync<Khaisinh2Model>(result).Result;
                                }

                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex);
                            throw;
                        }
                        return PartialView(Url, lg);
                    }
                    else
                    {
                        string Url = "~/Views/Online/ThongTinCaNhanPartial.cshtml";
                        ThongTinCaNhanModel ttcn = new ThongTinCaNhanModel();
                        try
                        {
                            using (WebClient wc = new WebClient())
                            {
                                if (ModelState.IsValid)
                                {
                                    Helper.SetHeaderInfo(wc, "UpdateKhaisinh1Back");
                                    wc.Encoding = Encoding.UTF8;
                                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateKhaisinh1Back");
                                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                    string result = wc.UploadString(UrlPath, "POST", json);
                                    ttcn = JsonConvert.DeserializeObjectAsync<ThongTinCaNhanModel>(result).Result;
                                }

                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex);
                            throw;
                        }
                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                        return PartialView(Url, ttcn);
                    }
                }
            }
            else
            {
                return Json(String.Empty);
            }
        }
        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult ThongTinCaNhan(ThongTinCaNhanModel model)
        {
            if (model != null)
            {
                if (model.Next == "1")
                {
                    string Url = "~/Views/Online/Khaisinh1Partial.cshtml";
                    Khaisinh1Model khaisinh1 = new Khaisinh1Model();
                    //save data

                    try
                    {
                        using (WebClient wc = new WebClient())
                        {
                            if (ModelState.IsValid)
                            {
                                Helper.SetHeaderInfo(wc, "UpdateThongTinCaNhan");
                                wc.Encoding = Encoding.UTF8;
                                wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateThongTinCaNhan");
                                var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                string result = wc.UploadString(UrlPath, "POST", json);
                                khaisinh1 = JsonConvert.DeserializeObjectAsync<Khaisinh1Model>(result).Result;
                            }

                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex);
                        throw;
                    }
                    //get data
                    ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                    return PartialView(Url, khaisinh1);
                }
                else
                {
                    string Url = "~/Views/Online/LoginKhaiOnlinePartial.cshtml";
                    Khaisinh1Model khaisinh1 = new Khaisinh1Model();
                    LoginKhaiOnlineViewModel lg = new LoginKhaiOnlineViewModel();
                    //save data

                    try
                    {
                        using (WebClient wc = new WebClient())
                        {
                            if (ModelState.IsValid)
                            {
                                Helper.SetHeaderInfo(wc, "UpdateThongTinCaNhan");
                                wc.Encoding = Encoding.UTF8;
                                wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateThongTinCaNhan");
                                var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                string result = wc.UploadString(UrlPath, "POST", json);
                                khaisinh1 = JsonConvert.DeserializeObjectAsync<Khaisinh1Model>(result).Result;
                            }

                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex);
                        throw;
                    }
                    //get data
                    return PartialView(Url, lg);
                }
            }
            else
            {
                return Json(String.Empty);
            }
            //else
            //{
            //    string Url = "~/Views/Online/ThongTinChungPartial.cshtml";
            //    ThongTinChungModel ttc = new ThongTinChungModel();
            //    //save data

            //    try
            //    {
            //        using (WebClient wc = new WebClient())
            //        {
            //            if (ModelState.IsValid)
            //            {
            //                wc.Encoding = Encoding.UTF8;
            //                wc.Headers["Content-type"] = "application/json; charset=utf-8";
            //                string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateThongTinCaNhanBack");
            //                var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
            //                string result = wc.UploadString(UrlPath, "POST", json);
            //                ttc = JsonConvert.DeserializeObjectAsync<ThongTinChungModel>(result).Result;
            //            }

            //        }
            //    }
            //    catch (Exception ex)
            //    {
            //        throw;
            //    }
            //    //get data
            //    ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
            //    return PartialView(Url, ttc);
            //}
        }
        //[HttpPost]
        //[AjaxValidateAntiForgeryToken]
        //public ActionResult ThongTinChung(ThongTinChungModel model)
        //{
        //    string Url = "~/Views/Online/ThongTinCaNhanPartial.cshtml";
        //    //save data
        //    //get data
        //    ThongTinCaNhanModel canhan = new ThongTinCaNhanModel();

        //    try
        //    {
        //        using (WebClient wc = new WebClient())
        //        {
        //            if (ModelState.IsValid)
        //            {
        //                wc.Encoding = Encoding.UTF8;
        //                wc.Headers["Content-type"] = "application/json; charset=utf-8";
        //                string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetThongtincanhan");
        //                var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
        //                string result = wc.UploadString(UrlPath, "POST", json);
        //                canhan = JsonConvert.DeserializeObjectAsync<ThongTinCaNhanModel>(result).Result;
        //            }
        //            else
        //            {
        //                ModelState.Values.SelectMany(v => v.Errors);
        //            }

        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        throw;
        //    }
        //    //set url theo next và pre
        //    ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
        //    return PartialView(Url, canhan);

        //}
        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult TinhTrangHonNhan(TinhTrangHonNhanModel model)
        {
            if (model != null)
            {
                if (model.Next == "1")
                {
                    string Url = "~/Views/Online/ThuongTru1Partial.cshtml";
                    ThuongTru1Model tt1 = new ThuongTru1Model();
                    try
                    {
                        using (WebClient wc = new WebClient())
                        {
                            if (ModelState.IsValid)
                            {
                                Helper.SetHeaderInfo(wc, "UpdateTinhTrangHonNhan");
                                wc.Encoding = Encoding.UTF8;
                                wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateTinhTrangHonNhan");
                                var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                string result = wc.UploadString(UrlPath, "POST", json);
                                tt1 = JsonConvert.DeserializeObjectAsync<ThuongTru1Model>(result).Result;
                            }
                            else
                            {
                                ModelState.Values.SelectMany(v => v.Errors);
                            }

                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex);
                        throw;
                    }

                    ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                    return PartialView(Url, tt1);
                }
                else
                {
                    if (model.Next == "2")
                    {
                        string Url = "~/Views/Online/LoginKhaiOnlinePartial.cshtml";
                        ThuongTru1Model tt1 = new ThuongTru1Model();
                        LoginKhaiOnlineViewModel lg = new LoginKhaiOnlineViewModel();
                        try
                        {
                            using (WebClient wc = new WebClient())
                            {
                                if (ModelState.IsValid)
                                {
                                    Helper.SetHeaderInfo(wc, "UpdateTinhTrangHonNhan");
                                    wc.Encoding = Encoding.UTF8;
                                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateTinhTrangHonNhan");
                                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                    string result = wc.UploadString(UrlPath, "POST", json);
                                    tt1 = JsonConvert.DeserializeObjectAsync<ThuongTru1Model>(result).Result;
                                }
                                else
                                {
                                    ModelState.Values.SelectMany(v => v.Errors);
                                }

                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex);
                            throw;
                        }
                        return PartialView(Url, lg);
                    }
                    else
                    {
                        string Url = "~/Views/Online/Khaisinh4Partial.cshtml";
                        Khaisinh4Model ks4 = new Khaisinh4Model();
                        try
                        {
                            using (WebClient wc = new WebClient())
                            {
                                if (ModelState.IsValid)
                                {
                                    Helper.SetHeaderInfo(wc, "UpdateTinhTrangHonNhanBack");
                                    wc.Encoding = Encoding.UTF8;
                                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateTinhTrangHonNhanBack");
                                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                    string result = wc.UploadString(UrlPath, "POST", json);
                                    ks4 = JsonConvert.DeserializeObjectAsync<Khaisinh4Model>(result).Result;
                                }
                                else
                                {
                                    ModelState.Values.SelectMany(v => v.Errors);
                                }

                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex);
                            throw;
                        }

                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                        return PartialView(Url, ks4);
                    }
                }
            }
            else
            {
                return Json(String.Empty);
            }
        }
        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult ThuongTru1(ThuongTru1Model model)
        {
            if (model != null)
            {
                if (model.Next == "1")
                {
                    string Url = "~/Views/Online/ThuongTru2Partial.cshtml";
                    ThuongTru2Model tt2 = new ThuongTru2Model();
                    try
                    {
                        using (WebClient wc = new WebClient())
                        {
                            if (ModelState.IsValid)
                            {
                                Helper.SetHeaderInfo(wc, "UpdateThuongTru1");
                                wc.Encoding = Encoding.UTF8;
                                wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateThuongTru1");
                                var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                string result = wc.UploadString(UrlPath, "POST", json);
                                tt2 = JsonConvert.DeserializeObjectAsync<ThuongTru2Model>(result).Result;
                            }
                            else
                            {
                                ModelState.Values.SelectMany(v => v.Errors);
                            }

                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex);
                        throw;
                    }

                    ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                    return PartialView(Url, tt2);
                }
                else
                {
                    if (model.Next == "2")
                    {
                        string Url = "~/Views/Online/LoginKhaiOnlinePartial.cshtml";
                        ThuongTru2Model tt2 = new ThuongTru2Model();
                        LoginKhaiOnlineViewModel lg = new LoginKhaiOnlineViewModel();
                        try
                        {
                            using (WebClient wc = new WebClient())
                            {
                                if (ModelState.IsValid)
                                {
                                    Helper.SetHeaderInfo(wc, "UpdateThuongTru1");
                                    wc.Encoding = Encoding.UTF8;
                                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateThuongTru1");
                                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                    string result = wc.UploadString(UrlPath, "POST", json);
                                    tt2 = JsonConvert.DeserializeObjectAsync<ThuongTru2Model>(result).Result;
                                }
                                else
                                {
                                    ModelState.Values.SelectMany(v => v.Errors);
                                }

                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex);
                            throw;
                        }
                        return PartialView(Url, lg);
                    }
                    else
                    {
                        string Url = "~/Views/Online/TinhTrangHonNhanPartial.cshtml";
                        TinhTrangHonNhanModel tthn = new TinhTrangHonNhanModel();
                        try
                        {
                            using (WebClient wc = new WebClient())
                            {
                                if (ModelState.IsValid)
                                {
                                    Helper.SetHeaderInfo(wc, "UpdateThuongTru1Back");
                                    wc.Encoding = Encoding.UTF8;
                                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateThuongTru1Back");
                                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                    string result = wc.UploadString(UrlPath, "POST", json);
                                    tthn = JsonConvert.DeserializeObjectAsync<TinhTrangHonNhanModel>(result).Result;
                                }
                                else
                                {
                                    ModelState.Values.SelectMany(v => v.Errors);
                                }

                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex);
                            throw;
                        }

                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                        return PartialView(Url, tthn);
                    }
                }
            }
            else
            {
                return Json(String.Empty);
            }
        }
        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult ThuongTru2(ThuongTru2Model model)
        {
            if (model != null)
            {
                if (model.Next == "1")
                {
                    string Url = "~/Views/Online/TheCanCuocPartial.cshtml";
                    TheCanCuocModel tcc = new TheCanCuocModel();
                    try
                    {
                        using (WebClient wc = new WebClient())
                        {
                            if (ModelState.IsValid)
                            {
                                Helper.SetHeaderInfo(wc, "UpdateThuongTru2");
                                wc.Encoding = Encoding.UTF8;
                                wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateThuongTru2");
                                var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                string result = wc.UploadString(UrlPath, "POST", json);
                                tcc = JsonConvert.DeserializeObjectAsync<TheCanCuocModel>(result).Result;
                            }
                            else
                            {
                                ModelState.Values.SelectMany(v => v.Errors);
                            }

                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex);
                        throw;
                    }
                    ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                    return PartialView(Url, tcc);
                }
                else
                {
                    if (model.Next == "2")
                    {
                        string Url = "~/Views/Online/LoginKhaiOnlinePartial.cshtml";
                        TheCanCuocModel tcc = new TheCanCuocModel();
                        LoginKhaiOnlineViewModel lg = new LoginKhaiOnlineViewModel();
                        try
                        {
                            using (WebClient wc = new WebClient())
                            {
                                if (ModelState.IsValid)
                                {
                                    Helper.SetHeaderInfo(wc, "UpdateThuongTru2");
                                    wc.Encoding = Encoding.UTF8;
                                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateThuongTru2");
                                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                    string result = wc.UploadString(UrlPath, "POST", json);
                                    tcc = JsonConvert.DeserializeObjectAsync<TheCanCuocModel>(result).Result;
                                }
                                else
                                {
                                    ModelState.Values.SelectMany(v => v.Errors);
                                }

                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex);
                            throw;
                        }

                        return PartialView(Url, lg);
                    }
                    else
                    {
                        string Url = "~/Views/Online/ThuongTru1Partial.cshtml";
                        ThuongTru1Model tt1 = new ThuongTru1Model();
                        try
                        {
                            using (WebClient wc = new WebClient())
                            {
                                if (ModelState.IsValid)
                                {
                                    Helper.SetHeaderInfo(wc, "UpdateThuongTru2Back");
                                    wc.Encoding = Encoding.UTF8;
                                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateThuongTru2Back");
                                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                    string result = wc.UploadString(UrlPath, "POST", json);
                                    tt1 = JsonConvert.DeserializeObjectAsync<ThuongTru1Model>(result).Result;
                                }
                                else
                                {
                                    ModelState.Values.SelectMany(v => v.Errors);
                                }

                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex);
                            throw;
                        }
                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                        return PartialView(Url, tt1);
                    }
                }
            }
            else
            {
                return Json(String.Empty);
            }
        }
        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult TheCanCuoc(TheCanCuocModel model)
        {
            if (model != null)
            {
                if (model.Next == "1")
                {
                    string Url = "~/Views/Online/HoChieuPartial.cshtml";
                    HoChieuModel hc = new HoChieuModel();
                    try
                    {
                        using (WebClient wc = new WebClient())
                        {
                            if (ModelState.IsValid)
                            {
                                Helper.SetHeaderInfo(wc, "UpdateTheCanCuoc");
                                wc.Encoding = Encoding.UTF8;
                                wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateTheCanCuoc");
                                var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                string result = wc.UploadString(UrlPath, "POST", json);
                                hc = JsonConvert.DeserializeObjectAsync<HoChieuModel>(result).Result;
                            }
                            else
                            {
                                ModelState.Values.SelectMany(v => v.Errors);
                            }

                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex);
                        throw;
                    }
                    return PartialView(Url, hc);
                }
                else
                {
                    if (model.Next == "2")
                    {

                        string Url = "~/Views/Online/LoginKhaiOnlinePartial.cshtml";
                        HoChieuModel hc = new HoChieuModel();
                        LoginKhaiOnlineViewModel lg = new LoginKhaiOnlineViewModel();
                        try
                        {
                            using (WebClient wc = new WebClient())
                            {
                                if (ModelState.IsValid)
                                {
                                    Helper.SetHeaderInfo(wc, "UpdateTheCanCuoc");
                                    wc.Encoding = Encoding.UTF8;
                                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateTheCanCuoc");
                                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                    string result = wc.UploadString(UrlPath, "POST", json);
                                    hc = JsonConvert.DeserializeObjectAsync<HoChieuModel>(result).Result;
                                }
                                else
                                {
                                    ModelState.Values.SelectMany(v => v.Errors);
                                }

                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex);
                            throw;
                        }
                        return PartialView(Url, lg);
                    }
                    else
                    {
                        string Url = "~/Views/Online/ThuongTru2Partial.cshtml";
                        ThuongTru2Model tt2 = new ThuongTru2Model();
                        try
                        {
                            using (WebClient wc = new WebClient())
                            {
                                if (ModelState.IsValid)
                                {
                                    Helper.SetHeaderInfo(wc, "UpdateTheCanCuocBack");
                                    wc.Encoding = Encoding.UTF8;
                                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateTheCanCuocBack");
                                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                    string result = wc.UploadString(UrlPath, "POST", json);
                                    tt2 = JsonConvert.DeserializeObjectAsync<ThuongTru2Model>(result).Result;
                                }
                                else
                                {
                                    ModelState.Values.SelectMany(v => v.Errors);
                                }

                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex);
                            throw;
                        }
                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                        return PartialView(Url, tt2);
                    }
                }
            }
            else
            {
                return Json(String.Empty);
            }
        }
        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult HoChieu(HoChieuModel model)
        {
            if (model != null)
            {
                if (model.Next == "1")
                {
                    string Url = "~/Views/Online/LevelPartial.cshtml";
                    LevelModel lv = new LevelModel();
                    try
                    {
                        using (WebClient wc = new WebClient())
                        {
                            if (ModelState.IsValid)
                            {
                                Helper.SetHeaderInfo(wc, "UpdateHoChieu");
                                wc.Encoding = Encoding.UTF8;
                                wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateHoChieu");
                                var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                string result = wc.UploadString(UrlPath, "POST", json);
                                lv = JsonConvert.DeserializeObjectAsync<LevelModel>(result).Result;
                            }
                            else
                            {
                                ModelState.Values.SelectMany(v => v.Errors);
                            }

                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex);
                        throw;
                    }
                    return PartialView(Url, lv);
                }
                else
                {
                    if (model.Next == "2")
                    {
                        string Url = "~/Views/Online/LoginKhaiOnlinePartial.cshtml";
                        LevelModel lv = new LevelModel();
                        LoginKhaiOnlineViewModel lg = new LoginKhaiOnlineViewModel();
                        try
                        {
                            using (WebClient wc = new WebClient())
                            {
                                if (ModelState.IsValid)
                                {
                                    Helper.SetHeaderInfo(wc, "UpdateHoChieu");
                                    wc.Encoding = Encoding.UTF8;
                                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateHoChieu");
                                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                    string result = wc.UploadString(UrlPath, "POST", json);
                                    lv = JsonConvert.DeserializeObjectAsync<LevelModel>(result).Result;
                                }
                                else
                                {
                                    ModelState.Values.SelectMany(v => v.Errors);
                                }

                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex);
                            throw;
                        }
                        return PartialView(Url, lg);
                    }
                    else
                    {
                        string Url = "~/Views/Online/TheCanCuocPartial.cshtml";
                        TheCanCuocModel tcc = new TheCanCuocModel();
                        try
                        {
                            using (WebClient wc = new WebClient())
                            {
                                if (ModelState.IsValid)
                                {
                                    Helper.SetHeaderInfo(wc, "UpdateHoChieuBack");
                                    wc.Encoding = Encoding.UTF8;
                                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateHoChieuBack");
                                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                    string result = wc.UploadString(UrlPath, "POST", json);
                                    tcc = JsonConvert.DeserializeObjectAsync<TheCanCuocModel>(result).Result;
                                }
                                else
                                {
                                    ModelState.Values.SelectMany(v => v.Errors);
                                }

                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex);
                            throw;
                        }
                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                        return PartialView(Url, tcc);
                    }
                }
            }
            else
            {
                return Json(String.Empty);
            }
        }
        #endregion

        #endregion

        #region Show medicine
        public ActionResult MedicinePartial()
        {
            return View();
        }
        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult MedicinePartial(MedicineModel model)
        {
            if (model != null)
            {
                if (model.Next == "1")
                {
                    string Url = "~/Views/Online/InsurancePartial.cshtml";
                    InsuranceModel insu = new InsuranceModel();
                    //save data

                    try
                    {
                        using (WebClient wc = new WebClient())
                        {
                            if (ModelState.IsValid)
                            {
                                Helper.SetHeaderInfo(wc, "UpdateMedicine");
                                wc.Encoding = Encoding.UTF8;
                                wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateMedicine");
                                var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                string result = wc.UploadString(UrlPath, "POST", json);
                                insu = JsonConvert.DeserializeObjectAsync<InsuranceModel>(result).Result;
                            }

                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex.ToString());
                        throw;
                    }
                    //get data
                    return PartialView(Url, insu);
                }
                else
                {
                    if (model.Next == "2")
                    {
                        string Url = "~/Views/Online/LoginKhaiOnlinePartial.cshtml";
                        InsuranceModel insu = new InsuranceModel();
                        LoginKhaiOnlineViewModel lg = new LoginKhaiOnlineViewModel();
                        //save data

                        try
                        {
                            using (WebClient wc = new WebClient())
                            {
                                if (ModelState.IsValid)
                                {
                                    Helper.SetHeaderInfo(wc, "UpdateMedicine");
                                    wc.Encoding = Encoding.UTF8;
                                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateMedicine");
                                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                    string result = wc.UploadString(UrlPath, "POST", json);
                                    insu = JsonConvert.DeserializeObjectAsync<InsuranceModel>(result).Result;
                                }

                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex.ToString());
                            throw;
                        }
                        //get data
                        return PartialView(Url, lg);
                    }
                    else
                    {
                        string Url = "~/Views/Online/LaborAndJobPartial.cshtml";
                        LaborAndJobModel la = new LaborAndJobModel();
                        //save data

                        try
                        {
                            using (WebClient wc = new WebClient())
                            {
                                if (ModelState.IsValid)
                                {
                                    Helper.SetHeaderInfo(wc, "UpdateMedicineBack");
                                    wc.Encoding = Encoding.UTF8;
                                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateMedicineBack");
                                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                    string result = wc.UploadString(UrlPath, "POST", json);
                                    la = JsonConvert.DeserializeObjectAsync<LaborAndJobModel>(result).Result;
                                }

                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex.ToString());
                            throw;
                        }
                        //get data
                        return PartialView(Url, la);
                    }
                }
            }
            else
            {
                return Json(String.Empty);
            }
        }
        #endregion

        #region Show insurance
        public ActionResult InsurancePartial()
        {
            return View();
        }

        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult InsurancePartial(InsuranceModel model)
        {
            if (model != null)
            {
                if (model.Next == "1")
                {
                    string Url = "~/Views/Online/ChetMattichPartial.cshtml";
                    //save data
                    ChetMatTichModel cm = new ChetMatTichModel();
                    try
                    {
                        using (WebClient wc = new WebClient())
                        {
                            if (ModelState.IsValid)
                            {
                                Helper.SetHeaderInfo(wc, "UpdateInsurance");
                                wc.Encoding = Encoding.UTF8;
                                wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateInsurance");
                                var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                string result = wc.UploadString(UrlPath, "POST", json);
                                cm = JsonConvert.DeserializeObjectAsync<ChetMatTichModel>(result).Result;
                            }

                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex.ToString());
                        throw;
                    }
                    //get data
                    return PartialView(Url, cm);
                }
                else
                {
                    if (model.Next == "2")
                    {
                        string Url = "~/Views/Online/LoginKhaiOnlinePartial.cshtml";
                        //save data
                        ChetMatTichModel cm = new ChetMatTichModel();
                        LoginKhaiOnlineViewModel lg = new LoginKhaiOnlineViewModel();
                        try
                        {
                            using (WebClient wc = new WebClient())
                            {
                                if (ModelState.IsValid)
                                {
                                    Helper.SetHeaderInfo(wc, "UpdateInsurance");
                                    wc.Encoding = Encoding.UTF8;
                                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateInsurance");
                                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                    string result = wc.UploadString(UrlPath, "POST", json);
                                    cm = JsonConvert.DeserializeObjectAsync<ChetMatTichModel>(result).Result;
                                }

                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex.ToString());
                            throw;
                        }
                        //get data
                        return PartialView(Url, lg);
                    }
                    else
                    {
                        string Url = "~/Views/Online/MedicinePartial.cshtml";
                        //save data
                        MedicineModel me = new MedicineModel();
                        try
                        {
                            using (WebClient wc = new WebClient())
                            {
                                if (ModelState.IsValid)
                                {
                                    Helper.SetHeaderInfo(wc, "UpdateInsuranceBack");
                                    wc.Encoding = Encoding.UTF8;
                                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateInsuranceBack");
                                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                    string result = wc.UploadString(UrlPath, "POST", json);
                                    me = JsonConvert.DeserializeObjectAsync<MedicineModel>(result).Result;
                                }

                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex.ToString());
                            throw;
                        }
                        //get data
                        return PartialView(Url, me);
                    }
                }
            }
            else
            {
                return Json(String.Empty);
            }
        }
        #endregion

        #region Show level
        public ActionResult LevelPartial()
        {
            return View();
        }
        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult LevelPartial(LevelModel model)
        {
            if (model != null)
            {
                if (model.Next == "1")
                {
                    string Url = "~/Views/Online/LaborAndJobPartial.cshtml";
                    LaborAndJobModel labor = new LaborAndJobModel();
                    //save data

                    try
                    {
                        using (WebClient wc = new WebClient())
                        {
                            if (ModelState.IsValid)
                            {
                                Helper.SetHeaderInfo(wc, "UpdateLevel");
                                wc.Encoding = Encoding.UTF8;
                                wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateLevel");
                                var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                string result = wc.UploadString(UrlPath, "POST", json);
                                labor = JsonConvert.DeserializeObjectAsync<LaborAndJobModel>(result).Result;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex.ToString());
                        throw;
                    }
                    //get data
                    return PartialView(Url, labor);
                }
                else
                {
                    if (model.Next == "2")
                    {
                        string Url = "~/Views/Online/LoginKhaiOnlinePartial.cshtml";
                        LaborAndJobModel labor = new LaborAndJobModel();
                        LoginKhaiOnlineViewModel lg = new LoginKhaiOnlineViewModel();
                        //save data

                        try
                        {
                            using (WebClient wc = new WebClient())
                            {
                                if (ModelState.IsValid)
                                {
                                    Helper.SetHeaderInfo(wc, "UpdateLevel");
                                    wc.Encoding = Encoding.UTF8;
                                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateLevel");
                                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                    string result = wc.UploadString(UrlPath, "POST", json);
                                    labor = JsonConvert.DeserializeObjectAsync<LaborAndJobModel>(result).Result;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex.ToString());
                            throw;
                        }
                        //get data
                        return PartialView(Url, lg);
                    }
                    else
                    {
                        string Url = "~/Views/Online/HoChieuPartial.cshtml";
                        HoChieuModel hc = new HoChieuModel();
                        //save data

                        try
                        {
                            using (WebClient wc = new WebClient())
                            {
                                if (ModelState.IsValid)
                                {
                                    Helper.SetHeaderInfo(wc, "UpdateLevelBack");
                                    wc.Encoding = Encoding.UTF8;
                                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateLevelBack");
                                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                    string result = wc.UploadString(UrlPath, "POST", json);
                                    hc = JsonConvert.DeserializeObjectAsync<HoChieuModel>(result).Result;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex.ToString());
                            throw;
                        }
                        //get data
                        return PartialView(Url, hc);
                    }
                }
            }
            else
            {
                return Json(String.Empty);
            }

        }
        #endregion

        #region Show labor and job
        public ActionResult LaborAndJobPartial()
        {
            return View();
        }
        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult LaborAndJobPartial(LaborAndJobModel model)
        {
            if (model != null)
            {
                if (model.Next == "1")
                {
                    string Url = "~/Views/Online/MedicinePartial.cshtml";
                    MedicineModel labor = new MedicineModel();
                    //save data

                    try
                    {
                        using (WebClient wc = new WebClient())
                        {
                            if (ModelState.IsValid)
                            {
                                Helper.SetHeaderInfo(wc, "UpdateLaborAndJob");
                                wc.Encoding = Encoding.UTF8;
                                wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateLaborAndJob");
                                var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                string result = wc.UploadString(UrlPath, "POST", json);
                                labor = JsonConvert.DeserializeObjectAsync<MedicineModel>(result).Result;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex.ToString());
                        throw;
                    }
                    //get data
                    return PartialView(Url, labor);
                }
                else
                {
                    if (model.Next == "2")
                    {
                        string Url = "~/Views/Online/LoginKhaiOnlinePartial.cshtml";
                        MedicineModel labor = new MedicineModel();
                        LoginKhaiOnlineViewModel lg = new LoginKhaiOnlineViewModel();
                        //save data

                        try
                        {
                            using (WebClient wc = new WebClient())
                            {
                                if (ModelState.IsValid)
                                {
                                    Helper.SetHeaderInfo(wc, "UpdateLaborAndJob");
                                    wc.Encoding = Encoding.UTF8;
                                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateLaborAndJob");
                                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                    string result = wc.UploadString(UrlPath, "POST", json);
                                    labor = JsonConvert.DeserializeObjectAsync<MedicineModel>(result).Result;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex.ToString());
                            throw;
                        }
                        //get data
                        return PartialView(Url, lg);
                    }
                    else
                    {
                        string Url = "~/Views/Online/LevelPartial.cshtml";
                        LevelModel lv = new LevelModel();
                        //save data

                        try
                        {
                            using (WebClient wc = new WebClient())
                            {
                                if (ModelState.IsValid)
                                {
                                    Helper.SetHeaderInfo(wc, "UpdateLaborAndJobBack");
                                    wc.Encoding = Encoding.UTF8;
                                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateLaborAndJobBack");
                                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                    string result = wc.UploadString(UrlPath, "POST", json);
                                    lv = JsonConvert.DeserializeObjectAsync<LevelModel>(result).Result;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex.ToString());
                            throw;
                        }
                        //get data
                        return PartialView(Url, lv);
                    }
                }
            }
            else
            {
                return Json(String.Empty);
            }

        }
        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult ChatMattich(ChetMatTichModel model)
        {
            if (model != null)
            {
                if (model.Next == "1")
                {
                    try
                    {
                        using (WebClient wc = new WebClient())
                        {
                            if (ModelState.IsValid)
                            {
                                Helper.SetHeaderInfo(wc, "UpdateChetMattich");
                                wc.Encoding = Encoding.UTF8;
                                wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateChetMattich");
                                var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                string result = wc.UploadString(UrlPath, "POST", json);

                            }
                        }

                        //string URL = String.Empty;
                        ThongTinChungModel ttc = new ThongTinChungModel();
                        if (model.ResidentNextId == "-1")
                        {
                            //return RedirectToAction("Submit", new { @houseHoldId = model.HOUSEHOLD_ID });
                            using (WebClient wc = new WebClient())
                            {
                                Helper.SetHeaderInfo(wc, "GetbyRESIDENTbyHOUSEHOLD_ID");
                                wc.Encoding = Encoding.UTF8;
                                wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetbyRESIDENTbyHOUSEHOLD_ID");
                                List<Resident1> lst = new List<Resident1>();
                                string json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model.HOUSEHOLD_ID);
                                string result = wc.UploadString(UrlPath, "POST", json);
                                lst = JsonConvert.DeserializeObjectAsync<List<Resident1>>(result).Result;
                                return PartialView("~/Views/Online/SubmitPartial.cshtml", lst);
                            }
                            //URL = "~/Views/Online/SubmitPartial.cshtml";
                        }
                        else
                        {
                            //URL = "~/Views/Online/ThongTinChungPartial.cshtml";
                            return RedirectToAction("KhaiOnline", new { @residentId = model.ResidentNextId });
                        }
                        //return PartialView(URL, ttc);
                        //get data
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex.ToString());
                        throw;
                    }
                }
                else
                {
                    if (model.Next == "2")
                    {
                        LoginKhaiOnlineViewModel lg = new LoginKhaiOnlineViewModel();
                        try
                        {
                            using (WebClient wc = new WebClient())
                            {
                                if (ModelState.IsValid)
                                {
                                    Helper.SetHeaderInfo(wc, "UpdateChetMattich");
                                    wc.Encoding = Encoding.UTF8;
                                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateChetMattich");
                                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                    string result = wc.UploadString(UrlPath, "POST", json);

                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex.ToString());
                            throw;
                        }
                        string URL = "~/Views/Online/LoginKhaiOnlinePartial.cshtml";
                        return PartialView(URL, lg);
                    }
                    else
                    {
                        string Url = "~/Views/Online/InsurancePartial.cshtml";
                        //save data
                        InsuranceModel insu = new InsuranceModel();
                        try
                        {
                            using (WebClient wc = new WebClient())
                            {
                                if (ModelState.IsValid)
                                {
                                    Helper.SetHeaderInfo(wc, "UpdateChetMattichBack");
                                    wc.Encoding = Encoding.UTF8;
                                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateChetMattichBack");
                                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                                    string result = wc.UploadString(UrlPath, "POST", json);
                                    insu = JsonConvert.DeserializeObjectAsync<InsuranceModel>(result).Result;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex.ToString());
                            throw;
                        }
                        //get data
                        return PartialView(Url, insu);
                    }
                }
            }
            else
            {
                return Json(String.Empty);
            }
        }
        public ActionResult Review()
        {
            return View();
        }
        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult Review(string RESIDENT_ID)
        {
            try
            {
                ThongTinCaNhanModel ttcn = new ThongTinCaNhanModel();
                using (WebClient wc = new WebClient())
                {
                    Helper.SetHeaderInfo(wc, "GetThongtincanhan");
                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetThongtincanhan");
                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(RESIDENT_ID);
                    string result = wc.UploadString(UrlPath, "POST", json);
                    ttcn = JsonConvert.DeserializeObjectAsync<ThongTinCaNhanModel>(result).Result;
                    ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                    return PartialView("~/Views/Online/ReviewPartial.cshtml", ttcn);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }
        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult ReviewBack(string HOUSEHOLD_ID)
        {
            try
            {
                using (WebClient wc = new WebClient())
                {
                    Helper.SetHeaderInfo(wc, "GetbyRESIDENTbyHOUSEHOLD_ID");
                    wc.Encoding = Encoding.UTF8;

                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                    string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetbyRESIDENTbyHOUSEHOLD_ID");
                    List<Resident1> lst = new List<Resident1>();
                    string json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(HOUSEHOLD_ID);
                    string result = wc.UploadString(UrlPath, "POST", json);
                    lst = JsonConvert.DeserializeObjectAsync<List<Resident1>>(result).Result;
                    return PartialView("~/Views/Online/SubmitPartial.cshtml", lst);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult Details(string RESIDENT_ID, string flat)
        {
            try
            {
                DAL.Models.Online.TTCOM a = new DAL.Models.Online.TTCOM();
                a.HOUSEHOLD_ID = MySessionUser.HouseholdId;
                a.RESIDENT_ID = RESIDENT_ID;
                using (WebClient wc = new WebClient())
                {

                    if (flat == "ttcn")
                    {
                        Helper.SetHeaderInfo(wc, "GetThongtincanhan");
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetThongtincanhan");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(a);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        ThongTinCaNhanModel ttcn = JsonConvert.DeserializeObjectAsync<ThongTinCaNhanModel>(result).Result;
                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                        return PartialView("~/Views/Online/ThongtincanhanNewPartial.cshtml", ttcn);
                    }
                    if (flat == "ttks")
                    {
                        Helper.SetHeaderInfo(wc, "GetKhaisinh1");
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetKhaisinh1");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(a);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        Khaisinh1Model ks1 = JsonConvert.DeserializeObjectAsync<Khaisinh1Model>(result).Result;
                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                        return PartialView("~/Views/Online/Khaisinh1NewPartial.cshtml", ks1);
                    }
                    if (flat == "ttcd")
                    {
                        Helper.SetHeaderInfo(wc, "GetKhaisinh2");
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetKhaisinh2");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(a);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        Khaisinh2Model ks2 = JsonConvert.DeserializeObjectAsync<Khaisinh2Model>(result).Result;
                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                        return PartialView("~/Views/Online/Khaisinh2NewPartial.cshtml", ks2);
                    }
                    if (flat == "ttmd")
                    {
                        Helper.SetHeaderInfo(wc, "GetKhaisinh3");
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetKhaisinh3");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(a);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        Khaisinh3Model ks3 = JsonConvert.DeserializeObjectAsync<Khaisinh3Model>(result).Result;
                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                        return PartialView("~/Views/Online/Khaisinh3NewPartial.cshtml", ks3);
                    }
                    if (flat == "ttnlh")
                    {
                        Helper.SetHeaderInfo(wc, "GetKhaisinh4");
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetKhaisinh4");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(a);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        Khaisinh4Model ks4 = JsonConvert.DeserializeObjectAsync<Khaisinh4Model>(result).Result;
                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                        return PartialView("~/Views/Online/Khaisinh4NewPartial.cshtml", ks4);
                    }
                    if (flat == "tttthn")
                    {
                        Helper.SetHeaderInfo(wc, "GetTinhtranghonnhan");
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetTinhtranghonnhan");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(a);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        TinhTrangHonNhanModel tthn = JsonConvert.DeserializeObjectAsync<TinhTrangHonNhanModel>(result).Result;
                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                        return PartialView("~/Views/Online/TinhTrangHonNhanNewPartial.cshtml", tthn);
                    }
                    if (flat == "tttt1")
                    {
                        Helper.SetHeaderInfo(wc, "GetThuongtru1");
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetThuongtru1");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(a);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        ThuongTru1Model ttt1 = JsonConvert.DeserializeObjectAsync<ThuongTru1Model>(result).Result;
                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                        return PartialView("~/Views/Online/ThuongTru1NewPartial.cshtml", ttt1);
                    }
                    if (flat == "tttt2")
                    {
                        Helper.SetHeaderInfo(wc, "GetThuongtru2");
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetThuongtru2");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(a);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        ThuongTru2Model ttt2 = JsonConvert.DeserializeObjectAsync<ThuongTru2Model>(result).Result;
                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                        return PartialView("~/Views/Online/ThuongTru2NewPartial.cshtml", ttt2);
                    }
                    if (flat == "tttcc")
                    {
                        Helper.SetHeaderInfo(wc, "GetThecancuoc");
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetThecancuoc");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(a);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        TheCanCuocModel tttcc2 = JsonConvert.DeserializeObjectAsync<TheCanCuocModel>(result).Result;
                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                        return PartialView("~/Views/Online/TheCanCuocNewPartial.cshtml", tttcc2);
                    }

                    if (flat == "ttlv")
                    {
                        Helper.SetHeaderInfo(wc, "GetLevel");
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetLevel");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(a);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        LevelModel ttlv = JsonConvert.DeserializeObjectAsync<LevelModel>(result).Result;
                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                        return PartialView("~/Views/Online/LevelNewPartial.cshtml", ttlv);
                    }
                    if (flat == "ttla")
                    {
                        Helper.SetHeaderInfo(wc, "GetLabor");
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetLabor");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(a);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        LaborAndJobModel ttla = JsonConvert.DeserializeObjectAsync<LaborAndJobModel>(result).Result;
                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                        return PartialView("~/Views/Online/LaborAndJobPartialNewPartial.cshtml", ttla);
                    }
                    if (flat == "ttme")
                    {
                        Helper.SetHeaderInfo(wc, "GetMedicine");
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetMedicine");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(a);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        MedicineModel ttme = JsonConvert.DeserializeObjectAsync<MedicineModel>(result).Result;
                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                        return PartialView("~/Views/Online/MedicineNewPartial.cshtml", ttme);
                    }
                    if (flat == "ttin")
                    {
                        Helper.SetHeaderInfo(wc, "GetInsurance");
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetInsurance");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(a);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        InsuranceModel ttin = JsonConvert.DeserializeObjectAsync<InsuranceModel>(result).Result;
                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                        return PartialView("~/Views/Online/InsuranceNewPartial.cshtml", ttin);
                    }


                    return View(String.Empty);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }

        }
        public ActionResult Submit(string houseHoldId)
        {
            try
            {
                SubmitModel model = new SubmitModel();
                model.HOUSEHOLD_ID = houseHoldId;
                return PartialView("SubmitPartial", model);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult Submit(SubmitModel update)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    //update 
                    #region update

                    //EthnicModels.Add(new EthnicModel() { CODE = 1, ETHNIC_ID });          
                    using (System.Net.WebClient wc = new System.Net.WebClient())
                    {
                        Helper.SetHeaderInfo(wc, "SubmitComplete");
                        wc.Encoding = System.Text.Encoding.UTF8;
                        // List<SelectListItem> lstItems = new List<SelectListItem>();

                         wc.DownloadString(serviceUrl + "OnlineService.svc/SubmitComplete/" + update.HOUSEHOLD_ID);

                    }
                    #endregion
                    LoginKhaiOnlineViewModel model = new LoginKhaiOnlineViewModel();

                    return PartialView("LoginKhaiOnlinePartial", model);
                }
                else
                {
                    return View();
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }
        #endregion
        public ActionResult Help()
        {
            return View();
        }
        #region Start Region Review
        public ActionResult ReviewHouseHold(string houseId)
        {
            try
            {
                ReviewHouseHoldModel model = new ReviewHouseHoldModel();
                return View(model);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }
        #endregion End Region Review

        #region NEW

        public void getResidentJsonList()
        {

            try
            {
                
                using (WebClient wc = new WebClient())
                {
                    Helper.SetHeaderInfo(wc, "GetbyRESIDENTbyHOUSEHOLD_ID");
                    wc.Encoding = Encoding.UTF8;
                    string UrlPath = string.Format("{0}{1}/{2}", serviceUrl, "OnlineService.svc/GetbyRESIDENTbyHOUSEHOLD_ID/", MySessionUser.HouseholdId);
                    string result = wc.DownloadString(UrlPath);
                    Response.Write(result);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }

        }


        [HttpGet]
        [AjaxValidateAntiForgeryToken]
        public ActionResult GetTTCN(string residentId)
        {

            string Url = "~/Views/Online/ThongtincanhanNewPartial.cshtml";
            ThongTinCaNhanModel ttcn = new ThongTinCaNhanModel();
            DAL.Models.Online.TTCOM model = new DAL.Models.Online.TTCOM();
            model.RESIDENT_ID = residentId;
            model.HOUSEHOLD_ID = MySessionUser.HouseholdId;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "GetThongtincanhan");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetThongtincanhan");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        ttcn = JsonConvert.DeserializeObjectAsync<ThongTinCaNhanModel>(result).Result;
                    }

                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
            ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
            return PartialView(Url, ttcn);
        }

        public Khaisinh1Model GetKhaisinh1(string value)
        {
            Khaisinh1Model ks1 = new Khaisinh1Model();
            DAL.Models.Online.TTCOM a = new DAL.Models.Online.TTCOM();
            a.RESIDENT_ID = value;
            a.HOUSEHOLD_ID = MySessionUser.HouseholdId;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "GetKhaisinh1");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetKhaisinh1");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(a);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        ks1 = JsonConvert.DeserializeObjectAsync<Khaisinh1Model>(result).Result;
                    }

                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
            return ks1;
        }

        public Khaisinh2Model GetKhaisinh2(string value)
        {
            Khaisinh2Model ks2 = new Khaisinh2Model();
            DAL.Models.Online.TTCOM a = new DAL.Models.Online.TTCOM();
            a.RESIDENT_ID = value;
            a.HOUSEHOLD_ID = MySessionUser.HouseholdId;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "GetKhaisinh2");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetKhaisinh2");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(a);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        ks2 = JsonConvert.DeserializeObjectAsync<Khaisinh2Model>(result).Result;
                    }

                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
            return ks2;
        }

        public Khaisinh3Model GetKhaisinh3(string value)
        {
            Khaisinh3Model ks3 = new Khaisinh3Model();
            DAL.Models.Online.TTCOM a = new DAL.Models.Online.TTCOM();
            a.RESIDENT_ID = value;
            a.HOUSEHOLD_ID = MySessionUser.HouseholdId;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "GetKhaisinh3");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetKhaisinh3");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(a);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        ks3 = JsonConvert.DeserializeObjectAsync<Khaisinh3Model>(result).Result;
                    }

                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
            return ks3;
        }

        public Khaisinh4Model GetKhaisinh4(string value)
        {
            Khaisinh4Model ks4 = new Khaisinh4Model();
            DAL.Models.Online.TTCOM a = new DAL.Models.Online.TTCOM();
            a.RESIDENT_ID = value;
            a.HOUSEHOLD_ID = MySessionUser.HouseholdId;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "GetKhaisinh4");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetKhaisinh4");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(a);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        ks4 = JsonConvert.DeserializeObjectAsync<Khaisinh4Model>(result).Result;
                    }

                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
            return ks4;
        }

        public TinhTrangHonNhanModel GetTTHN(string value)
        {
            TinhTrangHonNhanModel tthn = new TinhTrangHonNhanModel();
            DAL.Models.Online.TTCOM a = new DAL.Models.Online.TTCOM();
            a.RESIDENT_ID = value;
            a.HOUSEHOLD_ID = MySessionUser.HouseholdId;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "GetTinhtranghonnhan");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetTinhtranghonnhan");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(a);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        tthn = JsonConvert.DeserializeObjectAsync<TinhTrangHonNhanModel>(result).Result;
                    }

                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
            return tthn;
        }

        public ThuongTru1Model GetTTTT1(string value)
        {
            ThuongTru1Model ttt1 = new ThuongTru1Model();
            DAL.Models.Online.TTCOM a = new DAL.Models.Online.TTCOM();
            a.RESIDENT_ID = value;
            a.HOUSEHOLD_ID = MySessionUser.HouseholdId;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "GetThuongtru1");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetThuongtru1");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(a);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        ttt1 = JsonConvert.DeserializeObjectAsync<ThuongTru1Model>(result).Result;
                    }

                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
            return ttt1;
        }

        public ThuongTru2Model GetTTTT2(string value)
        {
            ThuongTru2Model ttt2 = new ThuongTru2Model();
            DAL.Models.Online.TTCOM a = new DAL.Models.Online.TTCOM();
            a.RESIDENT_ID = value;
            a.HOUSEHOLD_ID = MySessionUser.HouseholdId;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "GetThuongtru2");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetThuongtru2");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(a);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        ttt2 = JsonConvert.DeserializeObjectAsync<ThuongTru2Model>(result).Result;
                    }

                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
            return ttt2;
        }

        public TheCanCuocModel GetTTTCC(string value)
        {
            TheCanCuocModel tttcc = new TheCanCuocModel();
            DAL.Models.Online.TTCOM a = new DAL.Models.Online.TTCOM();
            a.RESIDENT_ID = value;
            a.HOUSEHOLD_ID = MySessionUser.HouseholdId;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "GetThecancuoc");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetThecancuoc");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(a);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        tttcc = JsonConvert.DeserializeObjectAsync<TheCanCuocModel>(result).Result;
                    }

                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
            return tttcc;
        }

        public HoChieuModel GetHochieu(string value)
        {
            HoChieuModel tthc = new HoChieuModel();
            DAL.Models.Online.TTCOM a = new DAL.Models.Online.TTCOM();
            a.RESIDENT_ID = value;
            a.HOUSEHOLD_ID = MySessionUser.HouseholdId;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "GetHochieu");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetHochieu");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(a);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        tthc = JsonConvert.DeserializeObjectAsync<HoChieuModel>(result).Result;
                    }

                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
            return tthc;
        }

        public LevelModel GetLevel(string value)
        {
            LevelModel lv = new LevelModel();
            DAL.Models.Online.TTCOM a = new DAL.Models.Online.TTCOM();
            a.RESIDENT_ID = value;
            a.HOUSEHOLD_ID = MySessionUser.HouseholdId;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "GetLevel");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetLevel");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(a);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        lv = JsonConvert.DeserializeObjectAsync<LevelModel>(result).Result;
                    }

                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
            return lv;
        }

        public LaborAndJobModel GetLab(string value)
        {
            LaborAndJobModel lb = new LaborAndJobModel();
            DAL.Models.Online.TTCOM a = new DAL.Models.Online.TTCOM();
            a.RESIDENT_ID = value;
            a.HOUSEHOLD_ID = MySessionUser.HouseholdId;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "GetLabor");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetLabor");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(a);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        lb = JsonConvert.DeserializeObjectAsync<LaborAndJobModel>(result).Result;
                    }

                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
            return lb;
        }

        public MedicineModel GetMe(string value)
        {
            MedicineModel me = new MedicineModel();
            DAL.Models.Online.TTCOM a = new DAL.Models.Online.TTCOM();
            a.RESIDENT_ID = value;
            a.HOUSEHOLD_ID = MySessionUser.HouseholdId;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "GetMedicine");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetMedicine");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(a);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        me = JsonConvert.DeserializeObjectAsync<MedicineModel>(result).Result;
                    }

                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
            return me;
        }

        public InsuranceModel GetIn(string value)
        {
            InsuranceModel ins = new InsuranceModel();
            DAL.Models.Online.TTCOM a = new DAL.Models.Online.TTCOM();
            a.RESIDENT_ID = value;
            a.HOUSEHOLD_ID = MySessionUser.HouseholdId;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "GetInsurance");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetInsurance");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(a);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        ins = JsonConvert.DeserializeObjectAsync<InsuranceModel>(result).Result;
                    }

                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
            return ins;
        }
        public ChetMatTichModel Getch(string value)
        {
            ChetMatTichModel ch = new ChetMatTichModel();
            DAL.Models.Online.TTCOM a = new DAL.Models.Online.TTCOM();
            a.RESIDENT_ID = value;
            a.HOUSEHOLD_ID = MySessionUser.HouseholdId;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "GetChetvaMattich");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetChetvaMattich");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(a);
                        string result = wc.UploadString(UrlPath, "POST", json);
                        ch = JsonConvert.DeserializeObjectAsync<ChetMatTichModel>(result).Result;
                    }

                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
            return ch;
        }
        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult UpdateThongtincanhanNew(ThongTinCaNhanModel ttcn)
        {
            if (ttcn != null)
            {
                ttcn.HOUSEHOLD_ID = MySessionUser.HouseholdId;
                try
                {
                    using (WebClient wc = new WebClient())
                    {
                        if (ModelState.IsValid)
                        {
                            Helper.SetHeaderInfo(wc, "UpdateThongTinCaNhanNew");
                            wc.Encoding = Encoding.UTF8;
                            wc.Headers["Content-type"] = "application/json; charset=utf-8";
                            string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateThongTinCaNhanNew");
                            var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(ttcn);
                            string result = wc.UploadString(UrlPath, "POST", json);
                        }
                    }

                    if (ttcn.Next == "1")
                    {
                        string Url = "~/Views/Online/Khaisinh1NewPartial.cshtml";
                        Khaisinh1Model khaisinh1 = GetKhaisinh1(ttcn.RESIDENT_ID);
                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                        return PartialView(Url, khaisinh1);
                    }
                    else
                    {
                        if (ttcn.Next == "2")
                        {
                            return RedirectToAction("LogOff", "Online");
                        }
                        else
                        {
                            return Json(String.Empty);
                        }
                    }
                }
                catch (Exception ex)
                {
                    logger.Error(ex.ToString());
                    throw;
                }
            }
            else
            {
                return Json(String.Empty);
            }
        }

        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult Khaisinh1New(Khaisinh1Model model)
        {
            model.HOUSEHOLD_ID = MySessionUser.HouseholdId;
            DAL.Models.Online.TTCOM modelnew = new DAL.Models.Online.TTCOM();
            modelnew.RESIDENT_ID = model.RESIDENT_ID;
            modelnew.HOUSEHOLD_ID = MySessionUser.HouseholdId;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "UpdateKhaisinh1New");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateKhaisinh1New");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                        string result = wc.UploadString(UrlPath, "POST", json);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
            if (model.Next == "1")
            {

                string Url = "~/Views/Online/Khaisinh2NewPartial.cshtml";
                Khaisinh2Model ks2 = GetKhaisinh2(model.RESIDENT_ID);
                //get data
                ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                return PartialView(Url, ks2);
            }
            else
            {
                if (model.Next == "0")
                {
                    string Url = "~/Views/Online/ThongtincanhanNewPartial.cshtml";
                    ThongTinCaNhanModel ttcn = new ThongTinCaNhanModel();
                    //save data

                    try
                    {
                        using (WebClient wc = new WebClient())
                        {
                            if (ModelState.IsValid)
                            {
                                Helper.SetHeaderInfo(wc, "GetThongtincanhan");
                                wc.Encoding = Encoding.UTF8;
                                wc.Headers["Content-type"] = "application/json; charset=utf-8";
                                string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/GetThongtincanhan");
                                var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(modelnew);
                                string result = wc.UploadString(UrlPath, "POST", json);
                                ttcn = JsonConvert.DeserializeObjectAsync<ThongTinCaNhanModel>(result).Result;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex.ToString());
                        throw;
                    }
                    //get data
                    ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                    return PartialView(Url, ttcn);
                }
                else
                {
                    if (model.Next == "3")
                    {
                        return Json(String.Empty);
                    }
                    else
                    {
                        return RedirectToAction("LogOff", "Online");
                    }

                }
            }
        }

        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult Khaisinh2New(Khaisinh2Model model)
        {
            model.HOUSEHOLD_ID = MySessionUser.HouseholdId;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "UpdateKhaisinh2New");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateKhaisinh2New");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                        wc.UploadString(UrlPath, "POST", json);
                    }

                }

                if (model.Next == "1")
                {
                    string Url = "~/Views/Online/Khaisinh3NewPartial.cshtml";
                    Khaisinh3Model ks3 = GetKhaisinh3(model.RESIDENT_ID);
                    //get data
                    ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                    return PartialView(Url, ks3);
                }
                else
                {
                    if (model.Next == "0")
                    {
                        string Url = "~/Views/Online/Khaisinh1NewPartial.cshtml";
                        Khaisinh1Model ks1 = GetKhaisinh1(model.RESIDENT_ID);
                        //get data
                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                        return PartialView(Url, ks1);
                    }
                    else
                    {
                        if (model.Next == "3")
                        {
                            return Json("dd");
                        }
                        else
                        {
                            return RedirectToAction("LogOff", "Online");
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult Khaisinh3New(Khaisinh3Model model)
        {
            model.HOUSEHOLD_ID = MySessionUser.HouseholdId;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "UpdateKhaisinh3New");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateKhaisinh3New");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                         wc.UploadString(UrlPath, "POST", json);
                    }

                }

                if (model.Next == "1")
                {
                    string Url = "~/Views/Online/Khaisinh4NewPartial.cshtml";
                    Khaisinh4Model ks4 = GetKhaisinh4(model.RESIDENT_ID);
                    //get data
                    ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                    return PartialView(Url, ks4);
                }
                else
                {
                    if (model.Next == "0")
                    {
                        string Url = "~/Views/Online/Khaisinh2NewPartial.cshtml";
                        Khaisinh2Model ks2 = GetKhaisinh2(model.RESIDENT_ID);
                        //get data
                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                        return PartialView(Url, ks2);
                    }
                    else
                    {
                        if (model.Next == "3")
                        {
                            return Json("dd");
                        }
                        else
                        {
                            return RedirectToAction("LogOff", "Online");
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }
        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult Khaisinh4New(Khaisinh4Model model)
        {
            model.HOUSEHOLD_ID = MySessionUser.HouseholdId;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "UpdateKhaisinh4New");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateKhaisinh4New");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                         wc.UploadString(UrlPath, "POST", json);
                    }

                }
                if (model.Next == "1")
                {
                    string Url = "~/Views/Online/TinhTrangHonNhanNewPartial.cshtml";
                    TinhTrangHonNhanModel tthn = GetTTHN(model.RESIDENT_ID);
                    //get data
                    ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                    return PartialView(Url, tthn);
                }
                else
                {
                    if (model.Next == "0")
                    {
                        string Url = "~/Views/Online/Khaisinh3NewPartial.cshtml";
                        Khaisinh3Model ks3 = GetKhaisinh3(model.RESIDENT_ID);
                        //get data
                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                        return PartialView(Url, ks3);
                    }
                    else
                    {
                        if (model.Next == "3")
                        {
                            return Json("dd");
                        }
                        else
                        {
                            return RedirectToAction("LogOff", "Online");
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }
        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult TinhTrangHonNhanNew(TinhTrangHonNhanModel model)
        {
            model.HOUSEHOLD_ID = MySessionUser.HouseholdId;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "UpdateTinhTrangHonNhanNew");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateTinhTrangHonNhanNew");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                         wc.UploadString(UrlPath, "POST", json);
                    }

                }

                if (model.Next == "1")
                {
                    string Url = "~/Views/Online/ThuongTru1NewPartial.cshtml";
                    ThuongTru1Model ttt1 = GetTTTT1(model.RESIDENT_ID);
                    //get data
                    ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                    return PartialView(Url, ttt1);
                }
                else
                {
                    if (model.Next == "0")
                    {
                        string Url = "~/Views/Online/Khaisinh4NewPartial.cshtml";
                        Khaisinh4Model ks4 = GetKhaisinh4(model.RESIDENT_ID);
                        //get data
                        ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                        return PartialView(Url, ks4);
                    }
                    else
                    {
                        if (model.Next == "3")
                        {
                            return Json(String.Empty);
                        }
                        else
                        {
                            return RedirectToAction("LogOff", "Online");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult ThuongTru1New(ThuongTru1Model model)
        {
            model.HOUSEHOLD_ID = MySessionUser.HouseholdId;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "UpdateThuongTru1New");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateThuongTru1New");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                         wc.UploadString(UrlPath, "POST", json);
                    }

                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
            if (model.Next == "1")
            {
                string Url = "~/Views/Online/ThuongTru2NewPartial.cshtml";
                ThuongTru2Model ttt2 = GetTTTT2(model.RESIDENT_ID);
                //get data
                ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                return PartialView(Url, ttt2);
            }
            else
            {
                if (model.Next == "0")
                {
                    string Url = "~/Views/Online/TinhTrangHonNhanNewPartial.cshtml";
                    TinhTrangHonNhanModel tthn = GetTTHN(model.RESIDENT_ID);
                    //get data
                    ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                    return PartialView(Url, tthn);
                }
                else
                {
                    if (model.Next == "3")
                    {
                        return Json(String.Empty);
                    }
                    else
                    {
                        return RedirectToAction("LogOff", "Online");
                    }
                }
            }
        }
        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult ThuongTru2New(ThuongTru2Model model)
        {
            model.HOUSEHOLD_ID = MySessionUser.HouseholdId;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "UpdateThuongTru2New");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateThuongTru2New");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                         wc.UploadString(UrlPath, "POST", json);
                    }

                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
            if (model.Next == "1")
            {
                string Url = "~/Views/Online/TheCanCuocNewPartial.cshtml";
                TheCanCuocModel ttt2 = GetTTTCC(model.RESIDENT_ID);
                //get data
                ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                return PartialView(Url, ttt2);
            }
            else
            {
                if (model.Next == "0")
                {
                    string Url = "~/Views/Online/ThuongTru1NewPartial.cshtml";
                    ThuongTru1Model tt1 = GetTTTT1(model.RESIDENT_ID);
                    //get data
                    ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                    return PartialView(Url, tt1);
                }
                else
                {
                    if (model.Next == "3")
                    {
                        return Json(String.Empty);
                    }
                    else
                    {
                        return RedirectToAction("LogOff", "Online");
                    }
                }
            }
        }
        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult TheCanCuocNew(TheCanCuocModel model)
        {
            model.HOUSEHOLD_ID = MySessionUser.HouseholdId;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "UpdateTheCanCuocNew");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateTheCanCuocNew");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                         wc.UploadString(UrlPath, "POST", json);
                    }

                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
            if (model.Next == "1")
            {
                string Url = "~/Views/Online/LevelNewPartial.cshtml";
                LevelModel lv = GetLevel(model.RESIDENT_ID);
                //get data
                ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                return PartialView(Url, lv);
            }
            else
            {
                if (model.Next == "0")
                {
                    string Url = "~/Views/Online/ThuongTru2NewPartial.cshtml";
                    ThuongTru2Model tt2 = GetTTTT2(model.RESIDENT_ID);
                    //get data
                    ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                    return PartialView(Url, tt2);
                }
                else
                {
                    if (model.Next == "3")
                    {
                        return Json(String.Empty);
                    }
                    else
                    {
                        return RedirectToAction("LogOff", "Online");
                    }

                }
            }
        }





        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult LevelPartialNew(LevelModel model)
        {
            model.HOUSEHOLD_ID = MySessionUser.HouseholdId;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "UpdateLevelNew");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateLevelNew");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                         wc.UploadString(UrlPath, "POST", json);
                    }

                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
            if (model.Next == "1")
            {
                string Url = "~/Views/Online/LaborAndJobPartialNewPartial.cshtml";
                LaborAndJobModel lb = GetLab(model.RESIDENT_ID);
                //get data
                ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                return PartialView(Url, lb);
            }
            else
            {
                if (model.Next == "0")
                {
                    string Url = "~/Views/Online/TheCanCuocNewPartial.cshtml";
                    TheCanCuocModel tcc = GetTTTCC(model.RESIDENT_ID);
                    //get data
                    ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                    return PartialView(Url, tcc);
                }
                else
                {
                    if (model.Next == "3")
                    {
                        return Json(String.Empty);
                    }
                    else
                    {
                        return RedirectToAction("LogOff", "Online");
                    }

                }
            }
        }

        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult LaborAndJobPartialNew(LaborAndJobModel model)
        {
            model.HOUSEHOLD_ID = MySessionUser.HouseholdId;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "UpdateLaborAndJobNew");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateLaborAndJobNew");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                         wc.UploadString(UrlPath, "POST", json);
                    }

                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
            if (model.Next == "1")
            {
                string Url = "~/Views/Online/MedicineNewPartial.cshtml";
                MedicineModel me = GetMe(model.RESIDENT_ID);
                //get data
                ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                return PartialView(Url, me);
            }
            else
            {
                if (model.Next == "0")
                {
                    string Url = "~/Views/Online/LevelNewPartial.cshtml";
                    LevelModel lv = GetLevel(model.RESIDENT_ID);
                    //get data
                    ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                    return PartialView(Url, lv);
                }
                else
                {
                    if (model.Next == "3")
                    {
                        return Json(String.Empty);
                    }
                    else
                    {
                        return RedirectToAction("LogOff", "Online");
                    }

                }
            }
        }


        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult MedicinePartialNew(MedicineModel model)
        {
            model.HOUSEHOLD_ID = MySessionUser.HouseholdId;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "UpdateMedicineNew");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateMedicineNew");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                         wc.UploadString(UrlPath, "POST", json);
                    }

                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
            if (model.Next == "1")
            {
                string Url = "~/Views/Online/InsuranceNewPartial.cshtml";
                InsuranceModel ins = GetIn(model.RESIDENT_ID);
                //get data
                ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                return PartialView(Url, ins);
            }
            else
            {
                if (model.Next == "0")
                {
                    string Url = "~/Views/Online/LaborAndJobPartialNewPartial.cshtml";
                    LaborAndJobModel lb = GetLab(model.RESIDENT_ID);
                    //get data
                    ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                    return PartialView(Url, lb);
                }
                else
                {
                    if (model.Next == "3")
                    {
                        return Json(String.Empty);
                    }
                    else
                    {
                        return RedirectToAction("LogOff", "Online");
                    }
                }
            }
        }



        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult InsurancePartialNew(InsuranceModel model)
        {
            model.HOUSEHOLD_ID = MySessionUser.HouseholdId;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "UpdateInsuranceNew");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateInsuranceNew");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                         wc.UploadString(UrlPath, "POST", json);
                    }

                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
            if (model.Next == "1")
            {
                return Json(String.Empty);
            }
            else
            {
                if (model.Next == "0")
                {
                    string Url = "~/Views/Online/MedicineNewPartial.cshtml";
                    MedicineModel me = GetMe(model.RESIDENT_ID);
                    //get data
                    ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                    return PartialView(Url, me);
                }
                else
                {
                    if (model.Next == "3")
                    {
                        return Json(String.Empty);
                    }
                    else
                    {
                        return RedirectToAction("LogOff", "Online");
                    }

                }
            }
        }

        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult ChatMattichNew(ChetMatTichModel model)
        {
            model.HOUSEHOLD_ID = MySessionUser.HouseholdId;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    if (ModelState.IsValid)
                    {
                        Helper.SetHeaderInfo(wc, "UpdateChetMattich");
                        wc.Encoding = Encoding.UTF8;
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        string UrlPath = string.Format("{0}{1}", serviceUrl, "OnlineService.svc/UpdateChetMattich");
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(model);
                        string result = wc.UploadString(UrlPath, "POST", json);
                    }

                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
            if (model.Next != "1")
            {
                string Url = "~/Views/Online/InsuranceNewPartial.cshtml";
                InsuranceModel ins = GetIn(model.RESIDENT_ID);
                //get data
                ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                return PartialView(Url, ins);
            }
            else
            {
                string Url = "~/Views/Online/ChetMattichNewPartial.cshtml";
                ChetMatTichModel ch = Getch(model.RESIDENT_ID);
                //get data
                ViewData[DISTRICT_CODE] = GetAdministrativeUnit(DEFAULT_CODE, LEVEL_DEFAULT);
                return PartialView(Url, ch);
            }
        }

        #endregion

        public ActionResult SubmitNew()
        {
            try
            {
                List<DHQGHN.PM2.Models.Online.Resident1> lst = new List<Resident1>();
                using (WebClient wc = new WebClient())
                {
                    Helper.SetHeaderInfo(wc, "GetbyRESIDENTbyHOUSEHOLD_ID");
                    wc.Encoding = Encoding.UTF8;
                    string UrlPath = string.Format("{0}{1}/{2}", serviceUrl, "OnlineService.svc/GetbyRESIDENTbyHOUSEHOLD_ID/", MySessionUser.HouseholdId);
                    string result = wc.DownloadString(UrlPath);
                    lst = JsonConvert.DeserializeObjectAsync<List<Resident1>>(result).Result;
                }
                return View(lst);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public void CompleteNew()
        {
            try
            {
                if (ModelState.IsValid)
                {
                    //update 

                    //EthnicModels.Add(new EthnicModel() { CODE = 1, ETHNIC_ID });          
                    using (System.Net.WebClient wc = new System.Net.WebClient())
                    {
                        Helper.SetHeaderInfo(wc, "SubmitComplete");
                        wc.Encoding = System.Text.Encoding.UTF8;
                        // List<SelectListItem> lstItems = new List<SelectListItem>();

                        wc.DownloadString(serviceUrl + "OnlineService.svc/SubmitComplete/" + MySessionUser.HouseholdId);

                    }

                }
                else
                {

                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        [HttpPost]
        [AjaxValidateAntiForgeryToken]
        public ActionResult ReviewNew(string RESIDENT_ID, string NAME)
        {
            try
            {
                ResidentReview rw = new ResidentReview();
                rw.RESIDENT_ID = RESIDENT_ID;
                rw.NAME = NAME;
                return PartialView("~/Views/Online/ReviewNewView.cshtml", rw);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        private string GetFunction()
        {
            return "Tạo mã đăng nhập - OnlineController";
        }
    }
}